Professional Documents
Culture Documents
Python 1
Python 1
Python 1
• b = 40.5
print("The type of b", type(b))
Output- The type of b <class 'float'>
• c = 1+3j
print("The type of c", type(c))
Output- The type of c <class 'complex'>
• Sequence Type
String
• The string can be defined as the sequence of characters
represented in the quotation marks. In Python, we can use single,
double, or triple quotes to define a string.
str = "string using double quotes"
print(str)
Output- string using double quotes
n = 10
if n % 2 == 0:
print("n is an even number")
n=5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")
Output- n is odd
• nested if: Nested if statements are an if statement inside another if
statement.
a=5
b = 10
c = 15
if a > b:
if a > c:
print("a value is big")
else:
print("c value is big")
elif b > c:
print("b value is big")
else:
print("c is big")
• Output- c is big
• if-elif-else: The if-elif-else statement is used to conditionally
execute a statement or a block of statements.
x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than
y")
• for loop: A for loop is used to iterate over a sequence that is either
a list, tuple, dictionary, or a set. We can execute a set of statements
once for each item in a list, tuple, or dictionary.
lst = [1, 2, 3, 4, 5]
for i in range(len (lst)):
print(lst [i], end = " ")
for j in range(0,10):
print(j, end = " ")
Output
123450123456789
• while loop: In Python, while loops are used to execute a block of
statements repeatedly until a given condition is satisfied. Then, the
expression is checked again and, if it is still true, the body is
executed again. This continues until the expression becomes false.
m=5
i=0
while i < m:
print(i, end = " ")
i=i+1
print("End")
Output - 0 1 2 3 4 End
• for loop
• The for loop in Python is used to iterate the statements or a part of
the program several times. It is frequently used to traverse the data
structures like list, tuple, or dictionary.
• The syntax of for loop in python is given below.
• for iterating_var in sequence:
statement(s)
• For loop Using range() function
• The range() function
• The range() function is used to generate the sequence of the
numbers. If we pass the range(10), it will generate the numbers
from 0 to 9. The syntax of the range() function is given below.
• Syntax:
• range(start,stop,step size)
• The start represents the beginning of the iteration.
• The stop represents that the loop will iterate till stop-1.
The range(1,5) will generate numbers 1 to 4 iterations. It is
optional.
• The step size is used to skip the specific numbers from the iteration.
It is optional to use. By default, the step size is 1. It is optional.
• for i in range(10):
print(i,end = ' ')
• Output: - 0 1 2 3 4 5 6 7 8 9
Looping Through a String
• Since strings are arrays, we can loop through the characters in a
string, with a for loop.
• Example
• Loop through the letters in the word "banana":
• for x in "banana":
print(x)
• b
a
n
a
n
a
• Loop Through a List
• You can loop through the list items by using a for loop:
• Example
• Print all items in the list, one by one:
• thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Output-apple
banana
cherry
Loop Through a Dictionary
• You can loop through a dictionary by using a for loop.
• When looping through a dictionary, the return value are the keys of
the dictionary, but there are methods to return the values as well.
• Example
• Print all key names in the dictionary, one by one:
• thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
Output - brand
model
year
• Python Pass
• In Python, the pass keyword is used to execute nothing; it means,
when we don't want to execute code, the pass can be used to
execute empty. It is the same as the name refers to. It just makes
the control to pass by without executing any code. If we want to
bypass any code pass statement can be used.
• It is beneficial when a statement is required syntactically, but we
want we don't want to execute or execute it later. The difference
between the comments and pass is that, comments are entirely
ignored by the Python interpreter, where the pass statement is not
ignored.
• Suppose we have a loop, and we do not want to execute right this
moment, but we will execute in the future. Here we can use the
pass.
• Example - Pass statement
• # pass is just a placeholder for
for i in [1,2,3,4,5]:
if(i==4):
pass
print("This is pass block",i)
print(i)
• Output:
1
2
3
This is pass block 4
4
5
Python break statement
• The break is a keyword in python which is used to bring the
program control out of the loop. The break statement breaks the
loops one by one, i.e., in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops. In other words,
we can say that break is used to abort the current execution of the
program and the control goes to the next line after the loop.
• The break is commonly used in the cases where we need to break
the loop for a given condition.
• The syntax of the break is given below.
• #loop statements
• break;
list =[1,2,3,4]
count = 1;
for i in list:
if i == 4:
print("item matched")
count = count + 1;
break
print("found at",count,"location");
Output- item matched
found at 2 location
continue Statement
• The continue statement in Python is used to bring the program
control to the beginning of the loop. The continue statement skips
the remaining lines of code inside the loop and start with the next
iteration. It is mainly used for a particular condition inside the loop
so that we can skip some specific code for a particular condition.The
continue statement in Python is used to bring the program control
to the beginning of the loop. The continue statement skips the
remaining lines of code inside the loop and start with the next
iteration. It is mainly used for a particular condition inside the loop
so that we can skip some specific code for a particular condition.
• Syntax
#loop statements
continue
#the code to be skipped
i=0
while(i < 10):
i = i+1
if(i == 5):
continue
print(i)
Output- 1 2 3 4 6 7 8 9 10
my_list=[1,2,3,'oops','python']
my_list_2=list([4,5,3.14,'fun', 'joy'])
#print(my_list)
#print(my_list_2)
my_list_2.extend([555,12])
print(my_list_2)
print(len(my_list_2))
my_list.insert(1,'abc')
print(my_list)
print(my_list + ['just'])
print(my_list * 2)
print(my_list)
• #Delete Element
del my_list_2[3]
print(my_list_2)
my_list_2.remove('joy')
print((my_list_2))
a=my_list_2.pop(2)
print('popped element:', a ,'List Remaining :', my_list_2)
my_list_2.clear()
print(my_list_2)
print(my_list.count('Python'))
print(sorted(new))
print(new)
new.reverse()
print(new)
new_list = my_list.copy()
print(new_list)
• Set Operation
my_set={1, 2, 3, 4, 5, 5, 5,6}
print(my_set)
my_set.add(7)
print(my_set)
my_set1 = {1,2,3,4}
print(my_set1)
my_set2 = {3,4,5,6}
print(my_set2)
print(my_set1.union(my_set2),'------',my_set1 | my_set2)
print(my_set1.intersection(my_set2),'-----',my_set1 & my_set2)
print(my_set1.difference(my_set2),'-----',my_set1 - my_set2)
print(my_set1.symmetric_difference(my_set2),'-----',my_set1 ^ my_set2)
my_set1.clear()
print(my_set1)
Tuple Operation
my_tuple=()
my_tuple_2=tuple()
print(my_tuple)
print(my_tuple_2)
my_tuple=(1, 2, 3)
my_tuple_2 = tuple(('python','is','good'))
print(my_tuple)
print(my_tuple_2)
my_tuple = my_tuple + (4,5,6)
print(my_tuple)
my_tuple_3 = 'language',
print(my_tuple_3)
print(type(my_tuple_3))
#Accessing values
my_tuple=(1,2,3)
my_tuple_2 = ('python','is','good')
my_tuple_3 = (1,2, 3,['hi','Python'])
print(my_tuple[0])
print(my_tuple_2[:])
print(my_tuple_3[3][1])
my_tuple_3[3][0] = 'hello'
print(my_tuple_3)
print(my_tuple_3.count(2))
print(my_tuple_3.index(['hello','Python']))
• String Operation
my_str1 ='Python is oo language'
my_str2 ='python is easy'
print(my_str1)
print(my_str2)
print(my_str1[:])
print(my_str1[2:10])
print(my_str2[0:14:1])
print(my_str1[0:14:2])
print(my_str2[::-1])
print(my_str2[-7:-1])
my_str1 ='Python is oo language'
print(len(my_str1))
print(my_str1.count('t'))
print(my_str1.lower())
print(my_str1.upper())
print(my_str1.find('l'))
print(my_str1.partition('is'))
print(my_str1.split(' '))
print(my_str1.replace('oo','Object Oriented'))
• Dictionary Operation
my_dict = {}
my_dict_2 = dict()
print (my_dict)
print (my_dict_2)
#Accessing
my_dict = {'First':'Python','Second':'Java'}
print(my_dict['First'])
print(my_dict.get('Second'))
my_dict['Third']='Ruby'
print(my_dict)
a=my_dict.pop('Third')
print("Value" , a)
print(my_dict)
b=my_dict.popitem()
print(("Key, Value pair:", b))
print(my_dict)
my_dict.clear()
print(my_dict)
my_dict = {'First':'Python','Second':'Java'}
print((my_dict.keys()))
print(my_dict.values())
print(my_dict.items())
print(my_dict.get('First'))