0% found this document useful (0 votes)
3 views

python1

Uploaded by

ficajo8878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python1

Uploaded by

ficajo8878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Python Programming Full Stack Development

Milan Anant
Cyber security trainer
UNIT - 1
• Python is an open source, interpreted, high-level, general-purpose
programming language.

• Python is a dynamically typed and garbage-collected language.

• Python was Created by Guido van Rossum and first released in 1991.

• Python 2.0, released in 2000,

• 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

• Syntax is easier to read

• Open source

• Huge amount of additional open-source libraries


• A Python variable is a reserved memory location to store values.

• Unlike other programming languages, Python has no command for declaring a


variable.

• A variable is created the moment you first assign a value to it.

• Python uses Dynamic Typing so,

• Rules for variable name


• Name can not start with digit

• Space not allowed

• Can not contain special character

• Python keywords not allowed


• Example of Python variable:
1 x = 10
2 print(x)
3 print(type(x))
4
5 y = 123.456
6 print(y)
7
8 x = “milan"
9 print(x) n variable:
10 print(type(x))

Output
1 10
2 int
3 123.456
4 milan
5 str
String in python

• String is an Ordered Sequence of characters such as “milk”, ‘college’,

“cat” etc.

• String can be represented as single, double, or triple quotes.

• String in Python is immutable.

• Square brackets can be used to access elements of the string,


String functions in Python:
• Python has lots of built-in methods that you can use on strings, we are
going to cover some frequently used methods for strings like
• len()
• count()
• capitalize(), lower(), upper()
• istitle(), islower(), isupper()
• find(), rfind(), replace()
• index(), rindex() etc.
Note: len() is not the method of the string but can be used to get the length
of the string
Output : 5 (length of “milan”)
1 x = “milan"
2 print(len(x))
String Methods

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'

endindex will not be included in the


substring

1 x = milan anant,rajkot, gujarat, INDIA'


2 subx1 = x[0:5]
Output : ‘milan’
3 subx2 = x[12:17]
4 subx3 = x[21:]
5 subx4 = x[::-1] Output :’ ‘r a j k o’
6 print(subx1)
7 print(subx2) Output : gujarat, INDIA'
8 print(subx3)
9 print(subx4) Output : ‘AIDNI, tarajug, tokjar, tnana nalim’
• str.format() is one of the string formatting methods in
Python3, which allows multiple substitutions and value
formatting.
• This method lets us concatenate elements within a string
through positional formatting.
1 x = '{} institute, rajkot'
2 y = x.format(‘milan')
3 print(y) Output : milan institute, rajkot
4

• We can specify multiple parameters to the function


x = '{} institute, {}'
1
2 y = x.format(‘milan','rajkot') Output : milan institute, rajkot
3 print(y)
• We can specify the order of parameters in the string
1 x = '{1} institute, {0}'
2 y = x.format(‘milan','rajkot') Output : rajkot institute, milan
3 print(y)

• We can also specify alias within the string to specify the order

1 x = '{collegename} institute, {cityname}' Output : milan institute, rajkot


2 print(x.format(collegename=‘milan',cityname='rajkot'))
List
• List is a mutable ordered sequence of objects, duplicate values are
allowed inside list. Output : institute (List index
starts with 0)
• List will be represented by square brackets [ ].
Output : 3 (length of the List)
1 my_list = [‘milan', 'institute', ’rakot']
2 print(my_list[1]) Output : [‘milan', 'institute', 'rajkot']
3 print(len(my_list)) Note : spelling of rajkot is updated
4 my_list[2] = "rajkot"
5 print(my_list)
6 print(my_list[-1]) Output : rajkot (-1 represent last element)

• 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

1 my_list = ['darshan', 'institute', 'rajkot','gujarat','INDIA']


2 print(my_list[1:3])
List Methods
• append() method will add element at the end of the list.
1 my_list = [‘milan', 'institute', 'rajkot'] Output : [‘milan', 'institute', 'rajkot',
2 my_list.append('gujarat') 'gujarat']
3 print(my_list)

• insert() method will add element at the specified index in the


list1 my_list = [‘milan', 'institute', 'rajkot'] Output : [‘milan', 'institute', 'of',
2 my_list.insert(2,'of') 'engineering', 'rajkot']
3 my_list.insert(3,'engineering')
4 print(my_list)

• extend() method will add one data structure (List or any) to


1 my_list1 = [‘milan', 'institute'] Output : [‘milan', 'institute', ‘rajkot',
current List
2 my_list2 = ['rajkot','gujarat'] ‘gujarat']
3 my_list1.extend(my_list2)
4 print(my_list1)
• pop() method will remove the last element from the list and
return it. = [‘milan', 'institute','rajkot']
1 my_list Output : [‘milan', 'institute‘]
2 temp = my_list.pop()
3 print(my_list)

• 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_list = [‘milan', 'institute', ’milan','rajkot'] Output : []


2 my_list.clear()
• clear() method will remove all the elements from the List
3 print(my_list)
• count() method will return the number of occurrence of the
specified element.
1 my_list = [‘milan', 'institute', ’milan','rajkot']
2 c = my_list.count(‘milan')
3 print(c) Output : 2

• reverse() method will reverse the elements of the List


1 my_list = [‘milan', 'institute','rajkot']
2 my_list.reverse()
3 print(my_list) Output : ['rajkot', ‘institute’,’milan']

• sort() method will sort the elements in the List


1 my_list = [‘milan', 'college','of','enginnering','rajkot']
2 my_list.sort() Output : ['college', 'enginnering’, ‘milan’,
3 print(my_list) 'of', 'rajkot']
Tuple

• Tuple is a immutable ordered sequence of objects, duplicate


values are allowed inside list.

• Tuple will be represented by round brackets ( ).

• Tuple is similar to List but List is mutable whereas Tuple is


Output : (‘’milan', 'institute', 'of', 'engineering',
'of', 'rajkot')
immutable.
Output : 3 (index of ‘engineering’)

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' }

Key value is Key value pairs is


seperated by : seperated by ,

1 my_dict = {'college’:”milan", 'city':"rajkot",'type':"engineering"}


2 print(my_dict['college'])
3 print(my_dict.get('city'))

values can be accessed using key inside square


brackets as well as using get() method
Output : milan
rajkot
Dictionary methods
• keys() method will return list of all the keys associated with the
Dictionary.
1 my_dict = {‘college’:”milan", 'city':"rajkot",'type':"engineering"}
2 print(my_dict.keys())
Output : ['college', 'city', 'type']

• values() method will return list of allOutput


the : values associated
[‘milan', 'rajkot', with
'engineering']
the Dictionary.
1 my_dict = {'college’:”milan", 'city':"rajkot",'type':"engineering"}
2 print(my_dict.values())

Output : [('college', ‘milan'),


• items() method will return list of tuples for each
('city', key value
'rajkot'), ('type',pair
associated with the Dictionary. 'engineering')]
1 my_dict = {'college’:”milan", 'city':"rajkot",'type':"engineering"}
2 print(my_dict.items())
Set

• Set is a unordered collection of unique objects.

• Set will be represented by curly brackets { }.


Output : {1, 2, 3, 5, 9}

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

• We can segregate python operators in the following groups


• Arithmetic operators

• Assignment operators

• Comparison operators

• Logical operators

• Identity operators

• Membership operators

• Bitwise operators
Arithmetic Operators

• Note : consider A = 10 and B = 3


Operator Description Example Output
+ Addition A+B 13
- Subtraction A-B 7
3.3333333333333
/ Division A/B
335
* Multiplication A*B 30
% Modulus return the remainder A%B 1
// Floor division returns the quotient A // B 3
10 * 10 * 10 =
** Exponentiation A ** B
1000
Logical Operators

• Note : consider A = 10 and B = 3

Operator Description Example Output


Returns True if both statements are
and
true
A > 5 and B < 5 True
Returns True if one of the
or
statements is true
A > 5 or B > 5 True
Negate the result, returns True if the
not
result is False
not ( A > 5 ) False
Identity & Member Operators
• Identity Operator
• Note : consider A = [1,2], B = [1,2] and C=A
Operator Description Example Output
Returns True if both variables are A is B FALSE
is
the same object A is C TRUE
Returns True if both variables are
is not
different object
A is not B TRUE

• 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

• if statement is written using the if keyword followed by


condition and colon(:) .
1 if some_condition :
2 # Code to execute when condition is true

if statement ends with :

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

Indentation (tab/whitespace) at the


beginning

Outpu fordemo2. Output :


t: py
fordemo1. 2
1 1 my_list = [1,2,3,4,5,6,7,8,9]
py 2 for list_item in my_list : 4
1 my_list = [1, 2, 3, 4] 2
3 if list_item % 2 == 0 : 6
2 for list_item in my_list : 3
4 print(list_item) 8
3 print(list_item) 4
While loop
• While loop will continue to execute block of code until some condition remains
True.

• For example,

• while felling hungry, keep eating


Syntax
• 1while have internet pack available, while
keep loop ends
watching with :
videos Output :
while some_condition : X is
2 # Code to execute in loop greater
Indentation (tab/whitespace) at the than 3
withelse.p
beginning y
1 x = 5
2 while x < 3 :
whiledemo.py 3 print(x)
Output :
1 x = 0 4 x += 1 # x+
0
2 while x < 3 : 5 + is valid in python
3 print(x) 1 6 else :
4 x += 1 # x++ is valid in python 2 print("X is greater than 3")
break, continue & pass keywords
• break : Breaks out of the current breakdemo.py
closest enclosing loop. 1 for temp in range(5) : Output :
2 if temp == 2 : 0
3 break 1
4
5 print(temp)

• continue : Goes to the top of the continuedemo.py Output :


current closest enclosing loop. 1 for temp in range(5) : 0
2 if temp == 2 : 1
3 continue 3
4
4
5 print(temp)

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.

You might also like