3) LogicalOperator-algo
3) LogicalOperator-algo
Week 03:
vision.seecs.edu.pk
Recap – week 02
• Logical Operators
• Algorithm
• Pseudocode
• Flowchart
String Slicing
• String starting index [0]
• String end index [len(str)-1] // string length minus 1
print(str[4]) # output: ?
print(str[0]) # output: ?
print(str[2:5]) # output: ?
5 print(str[5:]) # output: ?
String Slicing
• String starting index [0]
• String end index [len(str)-1] // string length minus 1
print(str[4]) # output: o
print(str[0]) # output: P
print(str[2:5]) # output: tho
6 print(str[5:]) # output: n String
String Slicing
• For a negative index, Python returns the character starting from the end of the string.
print(str[-1]) # output: g
print(str[-6:-1]) # output: Strin
print(str[-6:]) # output: String
print(str[:-6]) # output: Python
print(str[:]) # output: ?
print(str[ ]) # output: ?
print(str[-13:-1]) # output: ?
print(str[-13:]) # output: ?
7
Strings are Immutable
• Python strings are immutable. It means that you cannot change the string.
• You’ll get an error if you update one or more characters in a string
• In order to modify a string, you need to create a new string from the existing string
• For Example:
str = "Python String"
new_str = 'J' + str[1:]
print(new_str) # Output?
Comparison Operators - review
• When you compare two variable, Python returns the result as a Boolean value
• For Example: a=20, b=10
x=a>b
print(x) # output: True
______________________________
print(a >= b) # output: True
print(a < b) # output: False
print(a <= b) # output: False .
print(a == b) # output: False
print(a != b) # output: True
print('a' < ‘b’) # output: True
print('a' > 'b‘) # output: False
Boolean Function: bool()
• Use the bool() function to test if a value is True or False.
• For Example:
x = 100
bool(x) # returns True for any non-zero number (all negatives and positives)
______________________________
x=0
bool(x) # returns False for zero number
______________________________
x=''
bool(x) # returns False for empty string
______________________________
x = 'hello'
bool(x) # returns True for non-empty string
______________________________
Logical Operators
• Output of comparison operators or bool() function: ???
FUNCTION make_tea():
How to make tea?
SET water into kettle
FOR water heating, check:
1. Take water into kettle IF water is boiled THEN
2. Heat the water
3. Check if the water is boiled make tea
4. After boiling of water, make tea ELSE
continue boiling
END IF
END FOR
END FUNCTION
Algorithm
Pseudocode
• Pseudocode constructs/ keywords are used to describe the control flow of the algorithm.
• SEQUENCE represents linear tasks sequentially performed one after the other.
• WHILE a loop with a condition at its beginning.
• REPEAT-UNTIL a loop with a condition at the bottom.
• FOR another way of looping.
• IF-THEN-ELSE a conditional statement changing the flow of the algorithm.
• CASE the generalization form of IF-THEN-ELSE.
FUNCTION make_tea():
Pseudocode
SET water into kettle
Rules to write Pseudocode: FOR water heating, check:
IF water is boiled THEN
17
Algorithm vs Pseudocode vs Flowchart
• How to make tea?
FUNCTION make_tea():
FUNCTION SUM():
END FUNCTION
Algorithm vs Pseudocode
• Find if a character is Vowel:
Start
SET character type variable ch
Read ch from User
IF (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
|| ch == 'i' || ch == 'I' || ch == 'o' ||
ch == 'O' || ch == 'u' || ch == 'U' ) THEN
RETURN "Vowel“
END IF
END
Algorithm vs Pseudocode
• Find if a character is Vowel or Consonant:
1. Start
2. SET character type variable ch
3. Read ch from User
4. IF (ch == 'a' || ch == 'A' || ch == 'e' || ch ==
'E' || ch == 'i' || ch == 'I' || ch == 'o' ||
ch == 'O' || ch == 'u' || ch == 'U' ) THEN
5. RETURN "Vowel"
6. ELSE
7. Print "Consonant"
8. END IF
9. END
Algorithm vs Pseudocode
• Find if a character exists in a string: