Python
Python
f=format string
, will give space automatically and + will not give any space
Escape Sequence
String Methods
Function in Python
Conditional Statement
If,elif,else keyword
Match Keyword
Loops in Python
While Loop
For Loop
List
Dictionary
Exception Handling
else block get only executed when try block executed successfully
raise Keyword
Module or Library (We have to import it)
● random module
Command-line argument
sys module
sys.argv
sys.exit
slices
Packages
● cowsays package
Using pip install package_name : we can install package in the machine
requests package
pip install requests
__name__ will set __main__ only when we will call its file name where it present
If we are importing it in another file and executing other file name then __name__ is not
set to __main__ therefore the it will not call main() method
PEP 8 Python Enhancement Proposal
● Assert
with Keyword
student.csv
Here I can’t sort it because students is a list and it contains dictionary and each having 2 keys
Inside dictionary name we have to pass Key name quotes ‘-‘ or “-”
Key : is used to give the way that in which way we have to sort it
Above code is sorting using key name in reverse order
Lambda Function
csv Library
Giving Column and Row Name
Import sys
from PIL import Image
regexes Expression
re library
For Email ex malan@harward.edu
.+@.+ //s@a —>malan@harward
..*@..* //b@a —>malan@harward
r is used for raw string to remove the \n like extra meaning function
Here .edu@something still getting pass and not showing any error
_.EDU if capital then if will not work here we can directly make it lowercase white taking it as
input
And flags are
re.search(---)
https://twitter.com/sanketdeshmukh
\
Object Oriented Programming
Tuple : Tuple is immutable therefore we can call its value by indexing created using ( )
Tuple is not support item assignment/imutable so we can not change its value
after assign
List : Tuple is mutable therefore we can call its value by indexing created using [ ]
List support item assignment/mutable so we can change its value after assign
_ _ init_ _ ( ) Method are special method which influence the value of variable
Its also called as Dunder Method / Instance Method
raise Keyword
If user enter the space instead of name
_ _ methods _ _
1. _ _ init _ _
2. _ _name_ _ = _ _main_ _
3. _ _str _ _
_ _str _ _ Method to print object from its referrence as it printing only the Memory reference
It take only one argument that is self
properties : Its an property used to control the flow of class i.e getter and setter
Decorators :
@property :- getter
@instanceVariableName.setter
When we try to assing the instance variable value then it internally called the setter method
In the following code the setter, getter name are same so we have to give identical name to
instance variable inside the getter and setter so we are using the _ operator to avoid error
class Student:
def __init__(self,name,house):
if not name:
raise ValueError
self.name=name
self.house=house
def __str__(self):
return f"{self.name} is from {self.house}"
@property
def name(self):
return self._name
@name.setter
def name(self,name):
if not name:
raise ValueError("Missing name")
self._name=name
@property
def house(self):
return self._house
@house.setter
def house(self, house):
if house not in
["Griffindor","Hufflepuff","Ravenclaw","Slytherin"]:
raise ValueError("Invalid House")
self._house=house
def main():
student=get_student()
print(student)
def get_student():
name=input("Name : ")
house=input("House : ")
return Student(name,house)
if __name__=="__main__":
main()
class int(x, base=10)
class str(object=’ ‘)
str.lower()
str.strip([chars])
class list([iterable])
list.append(x)
class dict( _ _ _ )
Static Method
@staticmethod property
Inheritance
Method Overloading
object.__add__(self, others)
Set : No duplicate and sorted
global variable: We can read the global variable can not change it value
To modify the value of global variable we have to write
global variable_name
And you can use annotation to find error quickly using mypy
-> used to tell the return value of function None->It is not returning any thing
“““ —””” Use to automate the documentation Here we use 3rd party tool to generate the
document like pdf
Command Line Argument
argparse #ArgumentParser
Always run python program eding with -h //help
Unpacking
With List
With Dictionary
We can pass default value as shown in above picture if we are passing default value then we
can skip that argument as default value is present
*args : Takes variable no of positional argument ,
**kwargs : optional/ Named parameter
example
Map
map(function, iterable, …)
list comprehensions
filter
For sorting it
Dictionary Comprehensions
Enumerate
enumerate(iterable, start=0)
Here it is generating one row and returning it and our for loop is not getting stop
Yield is returning iterator
_ concept
You have to learn
● String Function
● Maths Fuction
● Datatypes
● Csv library