Strings and Conditional Statements in Python | Shraddha Khapra # Lec 02
print("Strings andCoditional Statments")
# string : datatype
# string cab be crated in '',"",and """ cots
# Escape Sequence Character are speacial charater for formating exp \t ,\n
"""
str_1 = "hello everyone,\nthis is my python tutariol"
print(str_1)
"""
# Basic Opraetion
# Concatenation (hello + world) use "+" to concatenate to strings
# Legenth of strings using "len(str)"
# Concatenation
"""
str1 = "hello everyone \t"
str2 = "this is my new tutatiol"
finalstr = str1 +str2
print(len(str1))
print(finalstr)
"""
# Legenth of strings
"""
str1 = "hello everyone"
len1 = len(str1)
print(len1)
"""
# Indexing : Checking index of strings
"""
str1 = "asadshmasuddin"
print(str1[10])
print(str1[9])
print(str1[4])
"""
# Slicing : Acsessing Parts of a string
# str = [startig index : ending index] **ending index is not incluedable
"""
str1 = "asadshamsuddin"
slicing = str1[0:9]
length = len(str1)
print(length)
print(slicing)
"""
# Negative Index in Slicing
# Negative : count from backward or slicing from backward it will start from opposite side with -1 to according to your legnth
"""
str1 = "apple"
Negative = str1[-4:-1]
print(Negative)
"""
# End with
# String Function : it will make opreation on string
# End with : check your string ends with subsrting then it will give you true
"""
str1 = "hello this my seconed lecture of apnacollege"
print(str1.endswith("college"))
"""
# Captlized first Character
"""
str1 = "learn python from apnaCollege"
print(str1.capitalize()) #it deos not take any argument it will not old string instead craete new string
#if want to your original string then
str1 = (str1.capitalize())
print(str1)
"""
# Replace function in string replace of all occurrences of a
"""
# str.replace(old , new)
str1 = "hello this is asadshamsuddin"
replace = str1.replace("a" , "y")
replace = str1.replace("hello" , "hey")
print(replace)
"""
# Find function in string
# returns 1st index of first occurance * where it lies
# str.find(word)
"""
str1 = "hello everyone i am iron man"
find = str1.find("m")
find = str1.find("iron") #also find full word
print(find)
"""
#Occurance : Count occurance of subsring in a string
# str.count(word)
"""
str1 = "heloo this is wandaaaaaaa"
coun_occur = str1.count('a')
print(coun_occur)
"""
# Lets Practice
# WAP to input user first name & print its length
"""
usre_input = input("enter your first name = ")
user_len = len(usre_input)
print("Legeth of your first name is" , user_len)
"""
#WAP to find the occurance of "s" in string
"""
new_string = "This is$ my new string $ of this$ tut"
occurance = new_string.count("$")
print(occurance)
"""
# Conditional Statments
# if,elif,else SYNTAX
# if(condition)
# Statment1
# elif(condition)
# Statment2
# else
# StatmentN
""" lets practice """
"""
age = 23
if(age>=18):
print("you are eligible")
"""
# Difference b/w if and elif is our compiler check if eachtime but it will check elif when if statments wrong
"""
light_color = "yellow"
if(light_color == "red"):
print("Stop! You Can't go")
elif(light_color == "yellow"):
print("Look The Road!")
elif(light_color == "green"):
print("You may Go!")
print("Code Ends!")
"""
# Difference b/w if and elif is our compiler check if eachtime but it will check elif when if statments wrong
#EXP
"""
num = 10
if(num<20):
print("num is less than 20")
if(num<15):
print("num is less than 15") #it will checked and print each time outputs bcz we ahve used if statment each time
#If i write elif then it will check if statnent first if its wrong Go to elif statment
"""
#using else statments
# if all conditions will wrong then we use else statment
"""
light_color = "blue"
if(light_color == "red"):
print("Stop! You Can't go")
elif(light_color == "yellow"):
print("Look The Road!")
elif(light_color == "green"):
print("You may Go!")
else:
print("else run bcz all statments are wrong")
"""
#tab spaces are called indentatatio (proper spacing)
# Gradestudent based marks
"""
obtain_marks = int(input("Enter Your Marks ="))
if(obtain_marks>=90):
Grade = "A"
elif(obtain_marks>=80):
Grade = "B"
else:
Grade = "D"
print("Your Grade is =", Grade)
"""
# Nesting : Write a new condition in if statments
# if(condition):
# if(condition2):
# print(output)
"""
age = 12
if(age>=18):
if(age >=80):
print("You can't Drive")
else:
print("you can Drive")
else:
print("you are under 18")
"""
#Pracice Questions
# WAP to check number enterd by user is odd or even
"""
num = int(input("enter a number = "))
rem = num%2
if(rem == 0):
print("your number is even")
else:
print("number is odd")
"""
# WAP to find the gratest of 3 numbers enterd by user
"""
num1 = int(input("enter the 1st number = "))
num2 = int(input("enter the 2nd number = "))
num3 = int(input("enter the 3rd number = "))
if((num1>num2) and (num1>num3)):
print(num1,"is the largest number")
elif((num2>num1) and (num2>num3)):
print(num2, "is the largest number")
elif((num3>num1) and (num3>num2)):
print(num3 ,"is the largest number")
"""
# Solution 2
"""
a = int(input("enter a value ="))
b =int(input("enter a value ="))
c = int(input("enter a value ="))
if ((a>b) and (a>c)):
print(a,"is the largest number")
elif((b>a) and (b>c)):
print(b,"is the largest number")
else:
print(c,"is the largest number")
"""
# for four numbers
a=int(input("enter a value"))
b=int(input("enter a value"))
c=int(input("enter a value"))
d=int(input("enter a value"))
if((a>b)and(a>c)and(a>d)):
print(a, "is largest number")
elif((b>a)and(b>c)and(b>d)):
print(b,"is the largest number")
elif((c>a)and(c>b)and(c>d)):
print(c,"is the largest number")
else:
print(d,"is the largest number")
# WAP to check if num is multiple of 7
"""
num = int(input("enter a value ="))
modulas = num %7
if(modulas == 0):
print(num," is multiple of 7")
else:
print(num," is not multiple of 7")
"""
Comments
Post a Comment