python1
python1
Milan Anant
Cyber security trainer
UNIT - 1
• Python is an open source, interpreted, high-level, general-purpose
programming language.
• Python was Created by Guido van Rossum and first released in 1991.
• Python 3.0 was released in 2008 and the current version of Python is
3.8.3 (as of June 2020).
• The Python 2 language was officially discontinued in 2020open-source
• Python has many advantages
• Easy to learn
• Less code
• Open source
Output
1 10
2 int
3 123.456
4 milan
5 str
String in python
“cat” etc.
count() method will return the number of times a specified value occurs
in a string.
1 x = “milan"
Output : 1 (occurrence of ‘a’ in
2 ca = x.count('a')
3 print(ca) “milan”)
title(), lower(), upper() will returns capitalized, lower case and upper
case
1 x string respectively.
= " milan,Institute, rajkot"
Output : Milan, Institute, Rajkot
2 c = x.title()
3 l = x.lower()
4 u = x.upper() Output : milan, institute, rajkot
5 print(c)
6 print(l)
7 print(u) Output : MILAN INSTITUTE, RAJKOT
istitle(), islower(), isupper() will returns True if the given string is capitalized, lower
case and upper case respectively.
1 x = ’milan, institute, rajkot'
2 c = x.istitle() Output : False
3 l = x.islower()
4 u = x.isupper() Output : True
5 print(c)
6 print(l)
7 print(u)
Output : False
strip() method will remove whitespaces from both sides of the string and return the
string.
1 x = ' milan '
2 f = x.strip() Output : mialn(without space)
3 print(f)
rstrip() and lstrip() will remove whitespaces from right and left side respectively.
• find() method will search the string and returns the index at which they
find the specified value
1 x = ’milan institute, rajkot, india' Output : 6 (occurrence of ‘in’ in x)
2 f = x.find('in')
3 print(f)
• rfind() will search the string and returns the last index at which they find
the specified value
Output : 24 last occurrence of ‘in’ in
1 x = ’milan institute, rajkot,india' x)
2 r = x.rfind('in')
3 print(r)
• isalnum() method will return true if all the characters in the string are
alphanumeric (i.e either alphabets or numeric).
1 x = ’milan2710' Output : True
2 f = x.isalnum()
3 print(f)
• isalpha() and isnumeric() will return true if all the characters in the string are
only alphabets and numeric respectively.
• isdecimal() will return true is all the characters in the string are decimal.
1 x = '123.5'
2 r = x.isdecimal() Output : True
3 print(r)
• Note : isnuberic() and isdigit() are almost same, you suppose to find the
difference as Home work assignment for the string methods.
• We can get the substring in python using string slicing, we can
specify start index, end index and steps (colon separated) to
slice theanant,
x = ’milan string.
rajkot, gujarat, INDIA'
• We can also specify alias within the string to specify the order
• We can use slicing similar to string in order to get the sub list from
the list. Output : ['institute', 'rajkot']
Note : end index not included
• remove()
1 my_list =method will remove
[‘milan', 'institute', first occurrence of specified
'darshan','rajkot']
element
2 my_list.remove(‘milan')
3 print(my_list) Output : ['institute', 'darshan', 'rajkot']
1 my_tuple = (‘milan','institute','of','engineering','of','rajkot')
2 print(my_tuple)
3 print(my_tuple.index('engineering')) Output : 2
4 print(my_tuple.count('of'))
5 print(my_tuple[-1]) Output : rajkot
Dictionary
• Dictionary is a unordered collection of key value pairs.
• Dictionary will be represented by curly brackets { }.
• Dictionary is mutable.
my_dict = { 'key1':'value1', 'key2':'value2' }
1 my_set = {1,1,1,2,2,5,3,9}
2 print(my_set)
• Set has many in-built methods such as add(), clear(), copy(), pop(),
remove() etc.. which are similar to methods we have previously seen.
• Only difference between Set and List is that Set will have only unique
elements and List can have duplicate elements.
Operators in python
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Arithmetic Operators
• Member Operator
• Note : consider A = 2 and B = [1,2,3]
Operator Description Example Output
Returns True if a sequence with
in the specified value is present in A in B TRUE
the object
Returns True if a sequence with
not in the specified value is not present A not in B FALSE
in the object
If statement
1 x = 10 Output
2
X is greater than 5
3 if x > 5 :
print("X is greater than 5"
4
)
If else statement
1 if some_condition :
2 # Code to execute when condition is true
3 else :
4 # Code to execute when condition is false
1 x = 3
2 Output
3 if x > 5 :
print("X is greater tha 1 X is less than 5
4
n 5")
5 else :
6 print("X is less than 5
")
If, elif and else statement
1 if some_condition_1 :
2 # Code to execute when condition 1 is true
3 elif some_condition_2 :
4 # Code to execute when condition 2 is true
5 else :
6 # Code to execute when both conditions are false
1 x = 10
2 if x > 12 : Output
3 print("X is greater tha 1 X is greater than 5
4 n 12")
elif x > 5 :
5 print("X is greater tha
6 n 5")
7 else :
print("X is less than 5
8 ")
For loop in python
• Many objects in python are iterable, meaning we can iterate over every
element in the object.
• such as every elements from the List, every characters from the string
etc..
• We can use for loop to execute block of code for each element of iterable
object.
Syntax For loop ends with :
1 for temp_item in iterable_object :
2 # Code to execute for each object in iterable
• For example,
passdemo.py
• Pass : Does nothing at all, will be Output :
1 for temp in range(5) : (nothing)
used as a placeholder in 2 pass
conditions where you don’t want
to write anything.