-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython02_strings_slicing_indexing_conditionalstatements.py
More file actions
134 lines (91 loc) · 3.69 KB
/
Copy pathpython02_strings_slicing_indexing_conditionalstatements.py
File metadata and controls
134 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# ##ESCAPE SEQUENCES CHARACTER
# # str1 = "This is a string."
# # str2 = 'This is also a string.'
# # str3 = """This is a string """
# # str4 = '''This is also a string.'''
# # "this is India's world"
# #\n - new line
# str1 = "This is string. \nWe are learning python."
# print(str1)
# # CONATINATION OF STRINGS
# str1 = "Hello"
# str2 = "World"
# str3 = str1 + " " + str2
# print(str3)
# # LENGTH OF A STRING
# str1 = "Hello World"
# print(len(str1))
# # Indexing
# str = "Hello World"
# print(str[0]) # H
# print(str[6]) # W
# print(str[-1]) # d
# print(str[-7]) # W
# # SLICING
# #str[ startingindex : ending index]
# str = "Hello World"
# var1 = str[6:11]
# print(var1) # World
# var2 = str[6 : len(str)]
# print(var2) # World
# var3 = str[0 : ]
# print(var3) # Hello World
# var4 = str[-5: -1]
# print(var4) # Worl
# # STRING FUNCTIONS
# str = "i am learning python from youtube."
# print(str.endswith("ube")) # True
# print(str)
# print(str.capitalize()) # I am learning python from youtube.
# str = str.capitalize()
# print(str) # I am learning python from youtube.
# str = str.replace("am" , "was")
# print(str) # I was learning python from youtube.
# print(str.find("from"))
# print(str.find("java"))
# print(str.count("a")) # 2
# print(str.count("n")) # 3
# # PRACTICE PROBLEMS
# # 1. Write a program to input user's name and print its length.
# name = input("Enter your Full Name: ")
# print("Length of your name" , name , "is" , len(name))
# # CONDITIONAL STATEMENTS
# age = float(input("Enter your age:"))
# if(age >= 18):
# print("Your are eligilble for applying for a driving license.")
# print("You are also eligible for voting.")
# else:
# print("Your are not eligilble for applying for a driving license.")
# print("You are also not eligible for voting.")
# print("wait for" , 18 - age , "years to apply for driving licesense and voter id.")
# # difference between if and elif is that if is used for the first condition and elif is used for the subsequent conditions. If the first condition is true, the code block under if will be executed and the rest of the conditions will be ignored. If the first condition is false, the code will check the next condition under elif and so on until it finds a true condition or reaches the end of the conditions.
# # Indentation : space or tab before a line of code is called indentation. It is used to define the scope of loops, functions, and other code blocks. In Python, indentation is crucial as it indicates which statements belong to which blocks of code. For example, in an if statement, the code block that should be executed if the condition is true must be indented under the if statement. If the indentation is not correct, it will result in an IndentationError.
# # Grade Calculation Program
# marks = float(input("Enter your marks:"))
# if(marks >= 90):
# grade = "A"
# elif(90 > marks >= 80):
# grade = "B"
# elif(80 > marks >= 70):
# grade = "C"
# elif(70 > marks >= 60):
# grade = "D"
# elif(marks<60):
# grade = "F"
# print("Your grade is:" , grade)
# # NESTING OF CONDITIONAL STATEMENTS
# age = float(input("Enter your age: "))
# if(age >= 18):
# if (age >= 80):
# print("You cannot drive the vehicle.")
# else:
# print("you can drive your vehicle.")
# else:
# print("You cannot drive your vehicle until you turn 18.")
# WAP to check if a number is a multiple of 7 or not.
num = float(input("Enter your number: "))
rem = num % 7
if(rem == 0):
print("Yes, your number is divisible by 7.")
else:
print("Your number is not divisible by 7.")