You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
print (word[0])#this is used to get one character from string
7
+
print("The ninth character is '%s'"%word[9])
8
+
print(word[-1])
9
+
print(word[-2])
10
+
print(word[-3])
11
+
print(word[-4])
12
+
print(word[1:4])#yth this is used to get one or more character from a string #this can also be used for [1,2,3] kind of strings
13
+
print(word[:4])#pyth
14
+
print(len(word[:4]))#this is used to find the length of the specified string
15
+
print("The lenght of the string is '%s'"%len(word))#this can be used to find the length of the string
16
+
print("The lenght of PRINT is '%s'"%len("print"))
17
+
print(word.index("t"))#2#this is used to find the positon of the character
18
+
print(word.index("p"))#0
19
+
print(word.count("p"))#counts the nos of specified letters
20
+
print("a occurs %d times"%word.count("a"))#occurence of the specified char
21
+
print(word[2:5:2])#to #this will skip 2nd character #this used for skiping a given no of terms#[start:stop:step]
22
+
print(word[2:5:3])#t #this will 2nd and 3rd character
23
+
print("The reverse of the string is '%s'"%word[::-1])#this is used to reverse the string
24
+
print("The characters with odd index are '%s'"%word[1::2]) #0-based indexing
25
+
print(word.lower())#this is to convert it into lowercase
26
+
print(word.upper())#this is to convert it into uppercase
27
+
print(word.startswith("pyth"))#this will print TRUE#this is used to find if a string starts with a specified string
28
+
print(word.startswith("data"))#FALSE
29
+
print(word.endswith("data"))#TRUE
30
+
words=word.split(" ")#this is used to split a string at the specified character 'here " "' and to make it a list
31
+
print(words)
32
+
ifword=="python data"andnumber==1:#we can use boolean operators 'and' & 'or'
33
+
print("The word is %s and the number is %d"%(word,number))#which allows to make us to make complex boolean expressions
34
+
ifnumber==1ornumber==2:
35
+
print("The number is either 1 or 2 ")
36
+
word1="python"
37
+
ifword1inwords:#this is used to check whether the specified object is in the iterable object container
38
+
print("Your word is either pyhton or data")
39
+
print((notFalse)==(True))#True#NOT is a boolean operator which inverts the expression
40
+
ifnotnumber:#this can also be use to check if the numbers is 0/1 i.e. true/false
41
+
print("Number is 1")#this won't print as the number is "not" zero
42
+
ifnumber:
43
+
print("The number is 1")#this will print as the number is 1
44
+
45
+
a,b=3,4
46
+
print(a**b/a)#this is used for operations
47
+
print(a==3)#True #this is used for checking if the value matches by printing if it's true/false
48
+
49
+
50
+
print("\t\t FIBONACCI SERIES\n")
51
+
a,b=0,1
52
+
whilea<1000:#instead of curly brakets like in c we use "Tab space" indentation(meaning tab space for keeping things in loop/function)
53
+
print(a,end=',')#end=',' is used to put a comma after everything before it
54
+
a,b=b,a+b#this type of equality is used for, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.
55
+
56
+
57
+
name="Harshit "#string
58
+
name+="Soni"#adding things in a string
59
+
age=18
60
+
college="Tsec"
61
+
print("\nMy name is %s"%name)
62
+
print("\nMy name is %s.And i'm %d.I'm currently studying in %s"%(name, age, college))
string="\nMy name is %s %s.And I'm %d.I'm currently studying in %s"#string representation
68
+
print(string%data)
69
+
#Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.
70
+
print("""\
71
+
\n message:this can be used for writing multiple line of string
72
+
do you wanna cont?
73
+
1.yes 2.no
74
+
""")#this can be used for mutliple lines of strings
75
+
76
+
#looping
77
+
78
+
x= [iforiinrange(10)]#this is use for making a list using 'for' loop and variable assignment
79
+
print(x)
80
+
81
+
forxinrange(3,8):
82
+
print(x)
83
+
84
+
forxinrange(3,8,2):
85
+
print(x)
86
+
87
+
#WHILE loop
88
+
count=0
89
+
whilecount<5:
90
+
print("NUmbers using while loop %d"%count)
91
+
count+=1
92
+
93
+
#break
94
+
count=0
95
+
while1:
96
+
print("NUmbers using break while looping %d"%count)
97
+
count+=1
98
+
ifcount>=5:
99
+
break
100
+
101
+
#contiune
102
+
forxinrange(10):
103
+
ifx%2==0:
104
+
continue#skips the choosen number/thing
105
+
print("Odd numbers using contiune and for loop %d"%x)#prints odd numbers
106
+
print("Number of odd numbers are %d"%count)
107
+
108
+
forxinrange(10):
109
+
ifnotx%2==0:
110
+
continue#skips the choosen number/thing
111
+
print("Even numbers using contiune and for loop %d"%x)#prints even numbers
112
+
113
+
114
+
# Prints out 0,1,2,3,4 and then it prints "count value reached 5"
115
+
# "else" can be used with loops
116
+
count=0
117
+
while(count<5):
118
+
print(count)
119
+
count+=1
120
+
else:
121
+
print("count value reached %d"%(count))
122
+
123
+
# Prints out 1,2,3,4
124
+
foriinrange(1, 10):
125
+
if(i%5==0):
126
+
break
127
+
print(i)
128
+
else:
129
+
print("this is not printed because for loop is terminated because of break but not due to fail in condition")
130
+
131
+
foriinrange(10):
132
+
print(i)
133
+
else:
134
+
print("this 10")
135
+
136
+
#Functions
137
+
138
+
defmy_function():#defining a function
139
+
print("This is a function.")
140
+
141
+
my_function()#calling a function
142
+
143
+
username="har"
144
+
greeting="shit"
145
+
defmy_function_with_args(username, greeting):
146
+
print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
147
+
148
+
my_function_with_args(username, greeting)
149
+
150
+
defadding_two_strings(username, greeting):
151
+
returnusername+greeting
152
+
print(adding_two_strings(username, greeting))
153
+
154
+
a=2
155
+
b=3
156
+
defsum_two_numbers(a, b):
157
+
returna+b
158
+
print(sum_two_numbers(a, b))
159
+
160
+
#classes and object
161
+
classMyclass:
162
+
variable="shit"
163
+
164
+
deffunction(self):
165
+
print("This mesage is inside the class.")
166
+
167
+
objectx=Myclass()#Now the variable "objectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".
168
+
print(objectx.variable)
169
+
objectx.function()#calling a function from a class
170
+
171
+
classMyClass:
172
+
variable="blah"
173
+
174
+
deffunction(self):
175
+
print("This is a message inside the class.")
176
+
177
+
myobjectx=MyClass()
178
+
myobjecty=MyClass()
179
+
180
+
myobjecty.variable="yackity"
181
+
182
+
# Then print out both values
183
+
print(myobjectx.variable)
184
+
print(myobjecty.variable)
185
+
186
+
# define the Vehicle class
187
+
classVehicle:
188
+
name=""
189
+
kind="car"
190
+
color=""
191
+
value=100.00
192
+
defdescription(self):
193
+
desc_str="%s is a %s %s worth $%.2f."% (self.name, self.color, self.kind, self.value)
194
+
returndesc_str
195
+
196
+
# your code goes here
197
+
car1=Vehicle()
198
+
car1.name="Fer"
199
+
car1.color="red"
200
+
car1.kind="convertible"
201
+
car1.value=60000.00
202
+
203
+
car2=Vehicle()
204
+
car2.name="Jump"
205
+
car2.color="blue"
206
+
car2.kind="van"
207
+
car2.value=10000.00
208
+
209
+
# test code
210
+
print(car1.description())
211
+
print(car2.description())
212
+
213
+
#modules
214
+
#modules are python files with extension .py
215
+
#Modules are imported from other modules using the "import" command.
216
+
217
+
#packages
218
+
#Each package in Python is a directory which MUST contain a special file called __init__.py.
219
+
#If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar.
0 commit comments