Unit 1 - Strings
CBSE Revision Notes
Class-11 Computer Science (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)
Strings
Strings: Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example
Str1=’Hello world’
Str2=”I Love Programming”
Comparison of two strings:
str1=input("Enter first string")
str2=input("Enter second string")
if str1 == str2:
print(“Strings are Equal”)
else:
print("Strings are not Equal")
Concatenation of strings: This can be done by simple “+” operator.
str1=input("Enter ist string: ")
str2=input("Enter second string: ")
print("Strings after concatenation", str1 + str2)
Substring:
s = "abcdefghijklmnopqrs"
# Loop over some indexes.
for n in range(2, 4):
# Print slices.
print(n, s[n])
print(n, s[n:n + 2])
print(n, s[n:n + 3])
print(n, s[n:n + 4:2])
print(n, s[n:n + 6:2])
Result of this program:
2 c
2 cd
2 cde
2 ce
2 ceg
3 d
3 de
3 def
3 df
3 dfh
In UML modeling, states represent the changing behavior of an object. A change of state is described using a transition to show a path between two states.
States
A state can contain other states, often called as nested states or substates. If you are modeling complex state machines, use nested states to separate detailed behavior into multiple levels. States can also contain actions that identify the tasks that can occur when an object is in a particular state. Among the most pervasive notions in computing is that of "state".
We define the state of a computation as a set of information sufficient to determine the future possible behaviors. In other words, the state tells us how the system can change. It also can tell us something about what has happened in the past, if we know it to have been started in a particular initial state. It doesn't usually tell exactly what has happened, but rather conveys some abstraction of what has happened.
A typical computational system starts with the initial state that embodies the input to the computation, and continues until termination, at which point the output can be extracted from the final state.
State types and regions
Every state is divided into compartments. The top compartment displays the name of the state. The compartment below the name compartment is the action compartment. The action compartment displays the do, entry, or exit activities that the state may contain. Each compartment below the activities compartment represents a region. State machines, composite states, and orthogonal states contain regions. A region can contain states, pseudostates, and transitions. Use regions to define nested states and transitions.
Transitions
A transition shows a path between states that indicates that a change of state is occurring. A trigger, a guard condition, and an effect are the three parts of a transition, all of which are optional.
A trigger is an event that must occur for a transition to start. A guard condition is a Boolean condition that must be true for a transition to occur. An effect is an action or activity that the object performs when a guard condition is satisfied.
Event type | Description |
Call | An object receives a request to invoke an operation. The invocation of the operation triggers a transition. |
Change | A Boolean condition is specified that triggers a transition when the condition is true. |
Signal | A specified message that, when received by an object, triggers a transition. |
Time | A specified period of time that must pass or an absolute time that triggers a transition. |
Example 1:
Let's consider an ATM system function where if the user enters the invalid password three times the account will be locked.
In this system, if the user enters a valid password in any of the first three attempts the user will be logged in successfully. If the user enters the invalid password in the first or second try, the user will be asked to re-enter the password. And finally, if the user enters incorrect password 3rd time, the account will be blocked.
State transition diagram
In the diagram whenever the user enters the correct PIN he is moved to access granted state, and if he enters the wrong password he is moved to next try and if he does the same for the 3rd time the account blocked state is reached.