Search This Blog

Python Branching

Python Branching
Break , Continue, Pass are python branching techniques.

Break:
Break statement terminates the loop containing it.
# break statement
for val in [1,2,3,4,5]:
    if val == 3:
        break
    print(val)

print("The end")
O/p:
1
2
The end


Continue Skips current iteration.
# break statement
for val in [1,2,3,4,5]:
    if val == 3:
        continue
    print(val)

print("The end")
O/p:
1
2
4
5
The end

Pass:
To pass without executing code.
for val in [1,2,3,4,5]:
    if val == 3:
        pass
        print ('this is pass block')
    print(val)

print("The end")
Op:
1
2
this is pass block
3
4
5
The end







170 Python Other topics

 13 Other Topics:

 Python Shallow Copy, Deep Copy, Copy using =Operator
#Copy using = operator
list1 = [[1,2],[3,4],[5,6]]

list2 = list1 # Creates variable list2 referring Object list1

print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))

list1[1][1]=10

print('list1 after modification:', list1)
print('list2 after modification:', list2)
print('ID of list2:', id(list2))
O/p:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 82228360
list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 10], [5, 6]]
ID of list2: 82228360

list1 and list2 has same ID. If we modify values any, changes will appear in the other also.

To overcome this we have,
Shallow copy 
Deep copy.

Copy module is used for both shallow and deep copy.

Shallow Copy:
# shallow copy
import copy
list1 = [[1,2],[3,4],[5,6]]
list2 = copy.copy(list1) #list2 is shallow copy of list1. ( it is created as new object ) 
          # which has reference of the original elements of list1.                          
print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))

list1[1][1]=10

print('list1 after modification:', list1)
print('list2 after modification:', list2)
print('ID of list2:', id(list2))

list1.append([7,8])

print('list1 after sublist addition:', list1)
print('list2 after sublist addition:', list2) # appended sublist wont reflect here
Output:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 93070824

list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 10], [5, 6]]
ID of list2: 93070696

list1 after sublist addition: [[1, 2], [3, 10], [5, 6], [7, 8]]
list2 after sublist addition: [[1, 2], [3, 10], [5, 6]]



Deep Copy:
# Deep copy
import copy
list1 = [[1,2],[3,4],[5,6]]
list2 = copy.deepcopy(list1) # list2 is deep copy of list1. ( it is created as new object)
# and recursively adds the copies of nested objects present in the original elements.
# means list2 elements wont get effected when list1 elements modified
print('list1 before modification:', list1)
print('list2 before modification:', list2)
print('ID of list1:', id(list1))
print('ID of list2:', id(list2))

list1[1][1]=10
print('list1 after modification:', list1)
print('list2 after modification:', list2)


list1.append([7,8])
print('list1 after sublist addition:', list1)
print('list2 after sublist addition:', list2)
Output:
list1 before modification: [[1, 2], [3, 4], [5, 6]]
list2 before modification: [[1, 2], [3, 4], [5, 6]]
ID of list1: 86296296
ID of list2: 86295816

list1 after modification: [[1, 2], [3, 10], [5, 6]]
list2 after modification: [[1, 2], [3, 4], [5, 6]]


list1 after sublist addition: [[1, 2], [3, 10], [5, 6], [7, 8]]
list2 after sublist addition: [[1, 2], [3, 4], [5, 6]]

162 Python Class

 12) Python Class

#!/usr/bin/python
# creating class
class Employee:
   'base class for all employees'
   
   def __init__(self, f_name, l_name, age):
      self.f_name = f_name
      self.l_name = l_name
      self.age = age
         
   def displayEmployee(self):
      print ("First Name : ", self.f_name, "Last Name :", self.l_name, "Age: ", self.age)

#creating instance of objects
"This would create first object of Employee class"
emp1 = Employee("John", "Cena", 45)
"This would create second object of Employee class"
emp2 = Employee("Peter", "Parker", 37)

# calling/accessing attributes
emp1.displayEmployee()
emp2.displayEmployee()
O/p:
First Name :  John Last Name : Cena Age:  45
First Name :  Peter Last Name : Parker Age:  37

Class Inheritance:

161 Python Errors & Exceptions

Python Errors & Exceptions

Syntax error:
i=[1,2,3]
for x in i print (x)
O/p:
    for x in i print (x)   
               ^
SyntaxError: invalid syntax

160 Python Dates

 10). Python Dates

from time import time
time() # gives ticks
o/p:
1617804792.9688406

import time
print(datetime.datetime.now())
time.sleep(5) #delay by 5sec
print(datetime.datetime.now())
O/p:
2021-04-07 20:01:56.065809
2021-04-07 20:02:01.067447

date = datetime.datetime.now().strftime("%d-%m-%y")
print(date, '\n')
O/p:
29-03-21

date1 = datetime.datetime.now().strftime("%d%m%y")
print(date1, '\n')
O/p:
290321

date2 = datetime.datetime.now().strftime("%Y")
print(date2, '\n')
O/p:
2021

date2 = datetime.datetime.now().strftime("%y")
print(date2)
O/p:
21

#adding or subtraing days to todays date
import datetime
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
print(yesterday)
O/p:
2021-04-06 20:18:56.601772

159 Python File Handling

 9) File Handling








158 Python Regular Expressions

 8). Regular Expressions

import re
line = "Python is a great language"

match = re.search('^P', line) # '^' Matches char start of the string
print(match)
O/p:
< re.Match object;span = (0, 1), match = 'P' >

match = re.search('e$', line) # '$' matches char end of the string
print(match)
O/p:
< re.Matchobject; span = (25, 26), match = 'e' >

match = re.search('[a-z]', line) # matches a single character
print(match)
< re.Match object; span = (1, 2), match = 'y' >

#example
line = "Python is a great language"
match = re.search('[^a-z]', line) # matches a single character (here we get 1st space)
print(match)
O/p:
< re.Match object; span = (6, 7), match = ' ' >

line = 'python is a Great Language'
match = re.search('[a-z]+[^a-z]+[a-z]+[a-z]', line) # [a-z] gives 1st char + [^a-z] takes space :o/p is 'python '
print(match) # + [a-z] matches 1 char + [a-z] matches 1 char :final o/p is:'python is'
< re.Match object; span = (0, 9), match = 'python is' >

match = re.search('py*', line) # Matches preceding character zero or more times
print(match)

line = 'aa aa aaa aaaa'
match = re.search('a{3,4}', line) # Matches the preceding character minimum m times, and maximum n times
print(match)
O/p:
< re.Match
object;
span = (6, 9), match = 'aaa' >


#exmaple
line = 'aa aa aaa aaaa'
match = re.search('a{4}', line) # Matches the preceding character exactly m times
print(match)
O/p:
< re.Match object; span = (10, 14), match = 'aaaa' >


#exmaple
line = 'ac abc abcd'
match = re.search('ab?', line) # Matches the preceding character zero or one times
print(match)
o/p:
< re.Match
object;
span = (0, 1), match = 'a' >

#exmaple
line1 = 'python is a Great Language'
match = re.search(r'[\w]+[\w]', line1) # \w Matches a word character (a-zA-Z0-9) :
match.group()
O/p:
'python'

#exmaple
match = re.search(r'[\w]+@[\w]+\.[\w]+', 'Please mail it to ayushi_wasthere@gmail.com')
match.group()
'ayushi_wasthere@gmail.com'


#exmaple
line1 = 'python is a Great Language'
match = re.search('\w*', line1) # Matches a word character (a-zA-Z0-9) :
print(match)
O/p:
< re.Matchobject; span = (0, 6), match = 'python' >



#example
match = re.search('\w+', line1) # Matches a word character (a-zA-Z0-9) :
print(match)
o/p:
< re.Match object; span = (0, 6), match = 'python' >


#example
line2 = '@python@ is a eay to learn language'
match = re.search('\@\w+\@', line2)
print(match)
O/p:
< re.Match object; span = (0, 8), match = '@python@' >

#example
match = re.search('\w+\@', line2)
print(match)
o/p:
< re.Match object; span = (1, 8), match = 'python@' >

#### logfile exmaple
line3 = '07-04-2021-22:00:00 dev login in error'
match = re.search('\w+', line3)
print(match)
O/P:
< re.Match object; span = (0, 2), match = '07' >


#exmaple
match = re.search('[a-z]+\w', line3)
print(match)
O/p:
< re.Match object; span = (20, 23), match = 'dev' >


#example
match = re.search('\d+', line3)
print(match)
O/p:
< re.Match object; span = (0, 2), match = '07' >


#exmaple
match = re.search('\d+-\d+-\d+', line3)
print(match)
O/p:
< re.Match object; span = (0, 10), match = '07-04-2021' >


#example
file = open(r'D:\0_Data\regex\regex1.txt', 'r+')
error = []
for i in file:
match = re.search('\d+-\d+-\d+', i)
error.append(match)
print(match)
O/p:
< re.Match object; span = (0, 10), match = '07-04-2021' >
< re.Match object; span = (0, 10), match = '08-04-2021' >
< re.Match object; span = (0, 10), match = '09-04-2021' >
< re.Match object; span = (0, 10), match = '10-04-2021' >
< re.Match object; span = (0, 10), match = '11-04-2021' >
< re.Match object; span = (0, 10), match = '12-04-2021' >

#example
file = open(r'D:\0_Data\regex\regex1.txt', 'r+')
error_date = []
for i in file:
match = re.search('\d+-\d+-\d+', i)
error_date.append(match.group())
error_date

O/p:
['07-04-2021',
'08-04-2021',
'09-04-2021',
'10-04-2021',
'11-04-2021',
'12-04-2021']

157 Python Functions

 7) Functions

1-Function is defined using the def keyword.
2-Arguments/parameters:
Arguments/parameters are specified in parenthesis after function name.
n number of arguments can be added.
3- function_docstring is optional.
4-return exists a fucntion

# Python fucntion syntax
def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]


# simple python function
def hi():
    print ('hello')

hi() #to call function


Pass by reference vs value:
Parameters/arguments in Python are passed by reference. 
Means if you change what a parameter refers to within a function, change also reflects back in the calling function. 
# Pass by reference vs value
def paas_by_ref( mylist,mylist1 ):
   "This changes a passed list into this function"
   mylist.append([1,2,3]);
   mylist1.extend([1,2,3]);
   print ("Values inside function: ", mylist)
   print ("Values inside function mylist1: ", mylist1)
   return

# calling paas_by_ref function
mylist = [10,20,30];
mylist1 = [10,20,30];
paas_by_ref( mylist, mylist1  );
# paas_by_ref(  );
print ("Values outside function: ", mylist)
print ("Values outside function mylist1: ", mylist1)


Return statement:
def return_func( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2
   print ("Inside function : ", total)
   return total;

# # Now you can call sum function
return_func( 10, 20 );

Function Arguments:
You can call a function by using following types of arguments:
Required arguments
Keyword arguments
Default arguments
Variable-length arguments

# Required arguments
def args_func( str ):
   "This prints a passed string into this function"
   print (str)
   return;

# Now you can call printme function
args_func()  # pass args here otherwise gets error


# keyword arguments
def args_func (name1, name2):
   "This prints a passed info into this function"
   print ("Name1: ", name1)
   print ("NAme2: ", name2)
   return;

# Call  function
args_func (name1='a', name2="b")

# Default arguments
def keyword_args (name1, name2='b'):
   "This prints a passed info into this function"
   print ("Name1: ", name1)
   print ("NAme2: ", name2)
   return;

# Call  function
keyword_args (name1='a')

# Variable length arguments
def args_func (name1, *name2):
   "This prints a passed info into this function"
   print ("Name1: ", name1)
   print ("Name2: ", name2)
   return;

# Call  function
# for *name2, we can pass 0 or n args
args_func ('a')


155 Python Loops

 5) Loops

5.1 for loop
for i in [1,2,3]:
    print (i)

for loop with else, 
ints = [1, 2, 3, 4]

for d in [0,1,2,3]:
    if d == 3:
        break
else:
    print("No num left.")
print("1 num left")


5.2) while loop
m=5
i=0
while i < m:
    i=i+1
    print(i)


5.3 Nested loops
Nested loops is “one loop inside another loop”. 

Python Decision making

 4) Decision making

Evaluates expression with outcome as True or False.

4.1 Block Structure and Whitespace
code executed when a specific condition met is defined in a "block."
block structure in python is done by indentation.
use 4 spaces(1TAB) for each level of block indentation.

If Statement:
#!/usr/bin/python
var1 = 10
if var1:
   print ("Got desired value")
   print (var1)

IF Else Statement:
var = 0
if var:
   print ("Got true expression value")
   print (var)
else:
   print ("Got false expression value")
   print (var)
O/p:
Got false expression value
0


elif statement:
if expression1:
   statement(s)
elif expression2:
   statement(s)
elif expression3:
   statement(s)
else:
   statement(s)


if elseif:
if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   elif:
      statement(s)
   elif expression4:
      statement(s)
else:
   statement(s)

Nested loops:
n = int(input('enter number :'))

if n % 2 != 0:
    print('Odd number')
elif n % 2 == 0:
    if (n >= 2 and n <= 99):
        print('Two digit even number')
    elif (n >= 100 and n <= 999):
        print ('3 digit even number')
    else:
        print('>= 4 digit  even number ')
else:
    print('done')

153 Python variables

 

3. Variables, Dynamic Typing, Dynamic binding

Variables:
A variable in Python is defined through assignment. Assign values to variables using ' = '.
name = 'person1'
age = 30

print ('name :', name)
print ('age :', age)
O/p:
name : person1
age : 30



Dynamic Typing:
No need to declare variable type, value that a variable points to has a type, the variable has no type.

Dynamic Binding or Late Binding

When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. The type of object is determined at the run time so this is known as dynamic binding.


Strong Typing:
Example:
'Python ' + str(3.7) 

152 Python Data Types

 2.0 Python Data Types

Python has five standard data types –

Numeric types:     Sequences:      Sets:                   Mappings:    Boolean:

Int                           str                      set                     dict

Long                       byte                   frozen set

Float                       list

Complex                 tuple 

 

1) Numeric types:

Integers
Integers are any numbers without decimals   

#Int assignment exmaple
a=1
print(a)
o/p:
1
                                           
Long
no long in python 3
                    
Float
# float
a = 1.0
type(a)
                       
Complex:
Complex literals can be created by using the notation x + yj
x =real component 
y = imaginary component.
                                    


2) Sequences
2.1 Strings
strings are sequence.
Strings are immutable.
[] are used for Indexing. Indexing starts at 0.
String literals can be defined with any of single quotes ('), double quotes (") or triple quotes (''' or """)


2.2 List
4 built-in data types in Python are List, Tuple Set and Dictionary.
List is a sequence, it can hold different objects. (no fixed type constraint )


Lists are mutable.
Lists are constructed with Square brackets0 - "[]"
We can use " : " to index string.

list1= [1, 'str', 1.2]


indexing:
    


2.3 Tuple:    
Tuples are constructed with Parentheses '()' 
Immutable
Example usage of tuples, in days of week, dates in calendar.
For Data integrity, tuples are used.

Tuples are faster compared to list.   
tup1 = (1,2,3)  # tuple creation
print (tup1[:2]) # indexing tuple
O/p:
(1, 2)tuple
byte (binary type)




3) Sets:
set
frozen set

Sets are unordered collection of unique elements.
Built in data type which stores collections of data.
Items in set are unordered, unchangeable, unique.
Set items can be of any data type.
s = set()   # set creation
s.add(1) # adding elemt to set
print(s)
Output:
{1}

set1= {'apple', 1, 2.3}           # set creaiton
print(set1)
Output:
{2.3, 'apple', 1}

set creation from list:

list1 = ['1', '2', '1','2','1','2']
set2 = set(list1)
print(set2)
Output:
{'2', '1'}

Access set items:
We, can't access items in a set by referring index or key. But, we can loop over. 
set1= {'apple', 1, 2.3}     # set creaiton

set1[0]
O/p:
TypeError: 'set' object is not subscriptable

set1= {'apple', 1, 2.3}     # set creaiton
for i in set1:
    print(i)
O/p:
1
2.3
apple

Add new item to set:
set1= {1, 2.3, 'Bng'}     # set creaiton

set1.add('new-item')    #adding item to set
print(set1)
O/p:
{1, 2.3, 'new-item', 'Bng'}


Joining Sets:
set1 = {'a', 'b', 'c'}
set2 = {1, 2, 3}

unioned_set = set1.union(set2)
print (unioned_set)
O/p:
{1, 2, 'a', 3, 'c', 'b'}



4)Mappings:
Dictionary 
2.5 Dictionaries:
Dictionary is a mapping.
Mappings are a collection of objects that are stored by a "key".
Mapping store objects by their relative position.

Dictionary consists of a key with an associated value. Value can be any python object
dict1 = {1:'key1', 'key2': 'value2', 'key3': 'value3'}
dict1
O/p:
{1: 'key1', 'key2': 'value2', 'key3': 'value3'}



Dictionary comprehension:
dict_comprehension = {x:x**2 for x in range(5)}
dict_comprehension
O/p:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


5) Boolean:
Boolean can be defined by typing True/False without quotes
# Boolean Example
bool1 = True
bool2 = False

print (type(bool1))
O/p:
<class 'bool'>



Note: Mutability:
int, float, bool, str, tuple, unicode  are immutable.
list, set, dict are mutable.