This document is a Python cheatsheet covering various operators, including arithmetic, assignment, comparison, and logical operators. It also introduces lists and dictionaries, detailing their creation, functions, and methods, along with basic operations like concatenation and selection of elements. Additionally, it briefly mentions NumPy arrays and their functionalities for scientific computing.
This document is a Python cheatsheet covering various operators, including arithmetic, assignment, comparison, and logical operators. It also introduces lists and dictionaries, detailing their creation, functions, and methods, along with basic operations like concatenation and selection of elements. Additionally, it briefly mentions NumPy arrays and their functionalities for scientific computing.
PYTHON
CHEATSHEET
Part 1
CoCer Te eye)
Operators
Arithmetic operators
102 + 37 #Add two numbers with +
102 - 37 # Subtract a number with -
4 * 6 # Multiply two numbers with *
22 / 7 # Divide a number by another with /
22 // 7 # Integer divide a number with //
3 *x WU # Raise to the power with **
22 % 7 # Returns 1 # Get the remainder
after division with %
Assignment operators
a = 5 # Assign a value to a
x[0] =1 # Change the value of an item in a
iia
a Guaranteed Interview CallsCer Te eye)
Operators
Numeric comparison operators
= 3 # Test for equality with ==
3 # Test for inequality with #
1 # Test greater than with >
3 # Test greater than or equal to with =
4 # Test less than with <
4 # Test less than or equal to with =
WWWW WW
aA wy
Logical operators
~(2 == 2) # Logical NOT with ~
(1 4 1) & (1 < 1) # Logical AND with &
(1 = 1) | (1 < 1) # Logical OR with |
(1 # 1) * C1 < 1) # Logical XOR with *
S Curriculum inclusive of Gen Al and ChatgptCer Te eye)
Getting started with lists
Creating lists
Alist is an ordered and changeable sequence of elements. It can
old integers, characters, floats, strings, and even objects.
| PPP
# Create lists with [], elements separated by commas
x = [1, 3, 2]
List functions and methods
# Return a sorted copy of the list x
sorted(x) # Returns [1, 2, 3]
# Sort the list in-place (replaces x)
x.sort() # Returns None
# Reverse the order of elements in x
veversed(x) # Returns [2, 3, 1]
# Reverse the list in-place
paee neato @e mr ae 'C-h alba Ce) 11}
# Count the number of element 2 in the list
x.count(2)Cer Te eye)
Selecting list elements
Python lists are zero-indexed (the first element has index 0). For
ranges, the first element is included, but the last is not.
an ‘ana cai ea
mur 5 "Cy
# Select the Oth element in the list
x[0] # 'a'
# Select the last element in the list
x[-1] # 'e'
# Select 1st (inclusive) to 3rd
(exclusive)
«{1:3] # ['b', 'c']
# Select the 2nd to the end
xf2:] # E'c', 'd', 'e']
# Select Oth to 3rd (exclusive)
x[:3] # ['a', 'b', 'c']Cer Te eye)
Concatenating lists
Define the list x and y
= [1, 3, 6]
= (10, 15, 21]
Concatenate Lists with +
+ y #1, 3, 6, 10, 15, 21]
Repeat List n times with *
«x #[1, 3, 6, 1, 3, 6, 1, 3, 6]
3] Get 1:1 On Demand Doubt Clearing SessionsCer Te eye)
Getting started with dictionaries
Creating dictionaries
Ne] (oid olar=laVac ike) (elo) ko R= l(a e(- (0 (mor a= OTL oa
lists indexed by position, dictionaries are indexed by their keys,
the names of which must be unique.
# Create a dictionary with {}
{'a': 1, 'b': 4, 'c': 9}
Dictionary functions and methods
# Define the dictionary
a = f{'a': 1, 'b': 2, 'e': 3}
# Get the keys
x. keys() # dict_keys(['a', 'b', 'c'])
beet) tM Sy
x.values(Q) # dict_values([1, 2, 3])
# Get a value from a dictionary by specifying
boat =mCNN
x['a'] #1
NNCer Te eye)
NumPy Arrays
Creating arrays
NumPy is a python package for scientific computing. It provides
a multidimensional array of objects and efficient operations on
them. To import NumPy, you can run this Python code import
numpy as np
# Convert a python list to a NumPy array
np.array([1, 2, 3]) # array([1, 2, 3])
# Return a sequence from start (inclusive) to end
(exclusive)
np.arange(1,5) # array([1, 2, 3, 41)
# Return a stepped sequence from start (inclusive)
to end (exclusive)
np.arange(1,5,2) # array([1, 3])
# Repeat values n times
np.repeat({i, 3, 6], 3) # array([1, 1, 1, 3, 3, 3,
6, 6, 6])
# Repeat values n times
np.tile([1, 3, 6], 3) # array([1, 3, 6, 1, 3, 6, 1,
aD)UFexs Iva a)
(SLearnbay
India’s Most Trusted
Programmes for Working Professionals
if ERICT Academy
‘BIT Guwahati
Executive program in Per Ve eles ew Role
Data Science & Al ee: et Cle (ag
Certification from IIT Guwahati Certification from IBM & Microsoft
2 days Campus Immersion at IIT
2 days Campus Immersion at IIT
Get Certification from
Tu
CUS) BV elses