PYTHON
Lets learn Something New
LECTURE 2
STRING
CONDITION
STRINGS
String is data type that stores a sequence of characters.
Basic Operations
concatenation
“hello” + “world” “helloworld”
• length of str
• len(str)
INDEXING
BUTEX_BEST
0123456789
• str = “BUTEX_BEST”
• str[0] is ‘B’, str[1] is ‘U’ ...
str[0] = ‘A’ #not allowed
SLICING
Accessing parts of a string
str[ starting_idx : (ending_idx ) ] #But eding idx not be count
str = “ApnaCollege”
str[ 1 : 4 ] is “pna”
str[ : 4 ] is same as str[ 0 : 4]
str[ 1 : ] is same as str[ 1 : len(str)]
SLICING
Negative Index
A p p l e
-5 -4 -3 -2 -1
str = “Apple”
str[ -3 : -1 ] is “pl”
STRING FUNCTIONS
str = “I am a coder.”
[Link](“er.“) #returns true if string ends with substr
[Link]( ) #capitalizes 1st char
[Link]( word ) #returns 1st index of 1st occurrence
[Link]( old, new ) #replaces all occurrences of old with new
[Link](“am“) #counts the occurrence of substr in string
LETS PRACTICE
• input user’s first name & print its length ..
• find the occurrence of ‘$’ in a String.
CONDITIONAL STATEMENTS
if-elif-else (SYNTAX)
if(condition) :
Statement1
elif(condition):
Statement2
else:
StatementN
CONDITIONAL STATEMENTS
Grade students based on marks
marks >= 90, grade = “A”
90 > marks >= 80, grade = “B”
80 > marks >= 70, grade = “C”
70 > marks, grade = “D”
LET‘S PRACTICE
[Link] if a number entered by the user is odd or even
If (num%2==0):
print(even)
Else:
print(odd)
LETS PRACTICE
2. find the greatest of 3 numbers entered by the user.
If(a>b) and (a>c):
print (a greater)
Elif (b>c):
print (b greater)
Else:
c
Print(c)
LETS PRACTICE
3. check if a number is a multiple of 7 or not.
if (x%7==0)