22
Python Cheatsheet 🔥
Cheatsheets are good when you want to revise some of the concepts, but not an idle way to start learning
I would recommend you to learn in depth from this course: Udemy Course
If you can self learn, then you can refer this Repository: Github
-
Python Cheatsheet
- Index
- Theory
- VSCODE extension:
- Making a virtual env:
- Why Virtual env?
- Comments in python
- Data Types:
- Naming conventions:
- Printing in Python:
- Numbers in Python:
- Using with variables:
- Variables makes it easy to understands the code.
- Strings in Python:
- Using directly with print:
- Taking Input
- Use
,
instead of+
This will auto seprate them by spaces. - Escape characters in python:
- Check the length of string:
- String indexing
- String Slicing
- String methods:
- Formatting strings:
- String Print alignment
- Lists in Python:
- Basic Usage
- Concat
- Append the list
- Poping objs
- Sorting
- Reverse
- Nested list
- Dictionaries in Python
- Basic Usage
- Tuples in Python
- Basic Usage
- Sets in Python
- Convert list to set
- File IO with Python
- init file obj
- read contents
- move the cursor/pointer
- read line by line
- close the file
- Using context manager
- different modes for file
- Chaining comparison operators:
- Python Statements:
- if, elif, else
- for loops
- while loops
- Statement in python
- Some useful operators
- range()
- enumerate()
- zip()
- in operator:
- min and max:
- List Comprehensions
- help function in python
- Functions in python
- Basic function with argument and default value
- *args and **kwargs
- lamda, filter and map
- Classes in python
- Basic implementation
- Inheritance
- Polymorphism
- Using Special methods
- Exception Handling
- try, except, finally, and else.
- Decorators
- Generators
- Useful Python modules you should look.
- Working with CSVs in python
- Working with pdfs in python
- Sending Emails with python
- Python is a scripting language.
- Scripting vs Compiled ? - Language like c++/java's code needs to compiled by its compiler, after compilation it is just machine level code. Where as in a scripting language its interpreter will be run the code one the spot one line at a time.
- Python
- Python for vscode
- Magic Python
- Arepl
Because managing python dependencies is a mess, this will install dependencies for that project only instead of globally.
-
python -m venv venv
this will create a venv folder in your directory. -
source ./venv/bin/activate
this will activate this virtual env.
- single line use
#
# single line comments
- multiline use
'''
. often called as docstring, as it is just to document function/classes.
'''
This is a example of
Multiline comment.
'''
- int # Whole Numbers
- float # Number with decimals.
- str # String
- list # ordered sequence of object
- dict # unordered key-value pairs.
- tup # ordered immutable seq. of objects.
- set # Unordered collection of unique objs.
- bool # Logical value True / False.
- None # no value
- Use underscore for variables.
- variables cannot Start with a number.
- Avoid special meaning keywords.
- Use snake case for functions.
- Use CamelCase for Classes names.
print("")
print(1+2) # Addition
print(3-2) # Subtraction
print(3*2) # Multiplication
print(3/2) # Division
print(3%2) # Mod.
print(3**2) # Power
print((3 + 10) * 15) # Using Braces.
a = 10
print(a)
# TO check the type:
print(type(a))
my_income = 100
tax_rate = 0.1
my_taxes = my_income*tax_rate
print("My income tax is",my_taxes)
String is nothing but ordered seq. of characters.
Note: Strings are immutable
print("Simple String")
print('Add quotes inside the "string" by using single quote.')
print("concat string "+"like this")
greeting = "Hello"
name = input("Enter your name: ")
print(greeting + ' ' + name)
print(greeting,name)
print("Hello\nWorld")
# or
print("""
---
Hello
world\
Yeahh!!
"Quotes"
'Single quote'
---
""")
print(len("Hey"))
a = "Hello"
a[0] Will return H
a[start:end:step]
a[0:2] # Start from 0th index till 2(excluding)
a[::2] # Step will be 2.
# We can use this to print a string backwards
s[::-1]
# Multiply Strings
a = 'H' * 10
# Upper Case a string
s.upper()
# Lower case
s.lower()
# Splitting
s.split('W')
s.split() # split via whitespace.
.format()
-
f""
F-string
print('The {2} {1} {0}'.format('fox','brown','quick'))
print('First Object: {a}, Second Object: {b}, Third Object: {c}'.format(a=1,b='Two',c=12.3))
num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:10.4f}")
left_alignment = "Left Text"
center_alignment = "Centered Text"
right_alignment = "Right Text"
print(f"{left_alignment : <20}|{center_alignment : ^15}|{right_alignment : >20}")
More about this: https://pyformat.info/
# Supports dynamic types, as it is python :)
my_list = [100,2.5,"Mohit"]
# len(my_list) for length
# Change objs:
my_list[0]=1000
Slicing is same as String slicing
a = [1,2,3]
b = [4,5]
c = a + b
my_list.append(10.8)
my_list.pop(index) # default index is -1, returns popped element.
a = [1,2,3]
a.sort() # in-place sort, it will modify the list, returns None
# Tip: use sorted(a) it will return the value instead of in-place
a = [1,2,3]
a.reverse() # also in-place
a = [1, 2, 3, [4,5,]]
print(a[3][1]) # Returns 5.
Unordered key-value mappings, basically you can have custom keys
Think it like, you can use this to make dynamic variables, where key will be the variable name.
Like list value can be any data type.
prices = {"apple":10, "orange":20.5}
print(prices)
print(prices["apple"])
print(prices.keys()) # get keys
print(prices.values()) # get values
print(prices.items()) # get items, return tuples with keys and values
print(prices.pop("apple")) # Pop the object
print(prices)
print(prices.clear()) # Clear All
print(prices)
print(prices.get("banana")) # it will check if it is present, return None if not.
print(prices.__contains__("apple")) # Returns true/false
Same as list, but immutable.
a = (1,2,2,4)
print(a)
# Interesting Fact: tuple supports only two methods:
a.count(2) # This can be use with list as well.
a.index(3) # This can be use with list as well.
Sets are an unordered collection of unique elements.
a = set()
a.add(1)
a.add(1)
a.add(1)
print(a) # {1}
a = [1,1,2,2,2,3,3,3]
a = set(a)
file = open("file.txt")
contents = file.read()
print(contents)
file.seek(0)
contents = file.readlines() # returns list of lines.
file.close()
with open("file.txt") as file:
print(file.read())
-
r
: Read -
r+
: Read and Write -
w
: Write (will override the file) -
w+
: Write + Read (will override the file) -
a
: Append the file
To chain ==, != <, >, >=, <= and is
these operators, we have these logical operators
- and
- or
- not
if 2 > 3 and 2 > 5:
print("I am inevitable")
if "hello" is "world" or "india" is "country":
print("Yeah!!")
if not 10 == 10:
print("Tough luck!" )
Indentation is important in the python.
loc = 'Bank'
if loc == 'Auto Shop':
print('Welcome to the Auto Shop!')
elif loc == 'Bank':
print('Welcome to the bank!')
else:
print('Where are you?')
# iterate list/string/tuple
l = [1,2,3]
for item in l:
print(item)
# extraction made easy
list2 = [(2,4),(6,8),(10,12)]
for t1,t2 in list2: # Same as dict. t1 will be key, t2 will be value.
print(t1) # will print 2,6,10
# Protip about dict: use .value() and .keys() for looping Values/keys.
in python we can use python with else statement.
x = 1
while x < 3:
print(f"x is {x}")
x = x + 1
else:
print("Uhhoo! x > 3")
- break: Breaks out of the current closest enclosing loop.
- continue: Goes to the top of the closest enclosing loop.
- pass: Does nothing at all, Programmers use this for placeholder.
def future_method():
# Todo: implement it later.
pass
while True:
break
for i in range(10):
if i == 5:
#omit
continue
print(i)
range is a generator.
Syntax: range(start,end,step)
Use directly with loops for iteration.
a = list(range(0,11,2)) # returns 0,2,4,..10
with help of this we can keep track of index and value.
a = [20,100,5,3,6]
for index,value in enumerate(a):
print(f"{index}\t{value}")
zip multiple lists.
a = [1,2,3]
b = [4,5,6]
for item in zip(a,b):
print(item)
a = [1,2,3]
print(3 in a) # True
a = [1,2,3]
print(min(a)) # 1
print(max(a)) # 3
Quicker and unique way to create lists.
# Grab every letter in string
lst = [x for x in 'word']
# Square numbers in range and turn into list
lst = [x**2 for x in range(0,11)]
# Check for even numbers in a range
lst = [x for x in range(11) if x % 2 == 0]
if you are lazy like me, want to learn documentation about specific inbuilt method via terminal, you can use help()
a = [1,2,3]
help(a.insert) # will print info about this method
# def keyword to define functions.
def say_hello(name="world"):
print(f"Hello {name}!")
# or return f"Hello {name}!" if you want to return it.
-
*args
: N number of arguments, returns tuple. -
**kwargs
: N number of keyword arguments, returns dict.
def total_income(*args, **kwargs):
print(f"Income for month, {kwargs['month']} is : {sum(args)}")
total_income(10,20,300,month="July")
#map
def square(num):
return num**2
my_nums = [1,2,3,4,5]
map(square,my_nums) # 1, 4, 9, 16, 25
# filter
def check_even(num):
return num % 2 == 0
nums = [0,1,2,3,4,5,6,7,8,9,10]
filter(check_even, nums) # 0, 2, 4, 6, 8, 10
# lets convert each of the above function to lambda.
map(lambda num:num**2,my_nums)
filter(lambda num:num%2==0, nums)
class Circle:
pi = 3.14
# Circle gets instantiated with a radius (default is 1)
def __init__(self, radius=1):
self.radius = radius
self.area = radius * radius * Circle.pi
# Method for resetting Radius
def setRadius(self, new_radius):
self.radius = new_radius
self.area = new_radius * new_radius * self.pi
# Method for getting Circumference
def getCircumference(self):
return self.radius * self.pi * 2
c = Circle()
print('Radius is: ',c.radius)
print('Area is: ',c.area)
print('Circumference is: ',c.getCircumference())
class Animal:
def __init__(self):
print("Animal created")
def whoAmI(self):
print("Animal")
def eat(self):
print("Eating")
class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print("Dog created")
def whoAmI(self):
print("Dog")
def bark(self):
print("Woof!")
class Animal:
def __init__(self, name): # Constructor of the class
self.name = name
def speak(self): # Abstract method, defined by convention only
raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal):
def speak(self):
return self.name+' says Woof!'
class Cat(Animal):
def speak(self):
return self.name+' says Meow!'
fido = Dog('Fido')
isis = Cat('Isis')
print(fido.speak())
print(isis.speak())
Just like __init__
we have more special methods.
class Book:
def __init__(self, title, author, pages):
print("A book is created")
self.title = title
self.author = author
self.pages = pages
def __str__(self):
return "Title: %s, author: %s, pages: %s" %(self.title, self.author, self.pages)
def __len__(self):
return self.pages
def __del__(self):
print("A book is destroyed")
book = Book("Python Rocks!", "Jose Portilla", 159)
#Special Methods
print(book)
print(len(book))
del book
def askint():
while True:
try:
val = int(input("Please enter an integer: "))
except:
# You can also expect specific error like TypeError or generic type Exception
print("Looks like you did not enter an integer!")
continue
else:
print("Yep that's an integer!")
break
finally:
print("Finally, I executed!")
print(val)
def new_decorator(func):
def wrap_func():
print("Code would be here, before executing the func")
func()
print("Code here will execute after the func()")
return wrap_func
@new_decorator
def func_needs_decorator():
print("This function is in need of a Decorator")
func_needs_decorator()
# Code would be here, before executing the func
# This function is in need of a Decorator
# Code here will execute after the func()
# Without generator
def get_me_cubes(n):
output_list = []
for i in range(n):
output_list.append(i**3)
return output_list
print(get_me_cubes(10))
# With generator
def generate_cubes(n):
for i in range(n):
yield i**3
print(generate_cubes(10))
- collections
- os
- shutil
- datetime
- math
- random
- pdb
- re
- timeit
- zipfile
# don't forget to install csv
import csv
data = open('example.csv',encoding="utf-8")
# passing encoding is important otherwise you will get the Unicode error.
csv_data = csv.reader(data)
# reading
data_lines = list(csv_data)
# writing
file_to_output = open('to_save_file.csv','w',newline='')
# use 'a' for append
csv_writer = csv.writer(file_to_output,delimiter=',')
csv_writer.writerow(['a','b','c'])
file_to_output.close()
# don't forget to use PyPDF2
import PyPDF2
f = open('Working_Business_Proposal.pdf','rb')
# we need to pass rb for binary files.
pdf_text = []
pdf_reader = PyPDF2.PdfFileReader(f)
for p in range(pdf_reader.numPages):
page = pdf_reader.getPage(p)
pdf_text.append(page.extractText())
import smtplib
smtp_object = smtplib.SMTP('smtp.gmail.com',587)
email = "[email protected]"
password = "yourpassword"
# Tip: search about how you generate app passwords.
smtp_object.login(email,password)
from_address = "[email protected]"
to_address = "[email protected]"
subject = "Subject"
message = "Message"
msg = "Subject: " + subject + '\n' + message
smtp_object.sendmail(from_address,to_address,msg)
smtp_object.quit()
22