Professional Documents
Culture Documents
output:
Break a string character to start on the next This is a line
\n line This is a second line
def
function_name(parameter): Initiate a function with an optional def yourName(x):
commands parameter print(x+1)
add_3_to = lambda y: y+3
print(add_3_to(4))
Copyright © 2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
a=[1, 4, -10, 6, 8]
for b in a:
if b<=0:
continue
print(b)
output:
1
4
Ignore a condition and execute the rest of 6
continue the loop 8
a=[1, 4, -10, 6, 8]
for b in a:
if b>=6:
break
print(b)
output:
1
Terminate the flow of a loop with a given 4
break condition -10
for b in a:
pass Ignore a set of prior instructions pass
try:
print(a)
except:
print("An error occured!")
Try a block of code, else, raise a defined
try, except exception output: An error occured!
try:
print(a)
except:
print(d)
finally:
print("You can't print an undefined
variable")
Execute a final code when the try and the output: You can't print an
finally except blocks fail undefined variable
a=7+2
if a<10:
Raise an exception that stops the command raise Exception("Oh! You didn't get
raise Exception() when execution isn't possible a score of 10")
import x Import a whole module or library import math
from x import y Import a library x from a file, or a class y from scipy.stats import mode
Customize an expression to your preferred
as name import pandas as pd
x=[1, 4, 6, 7]
if 5 in x:
print("There is a five")
else:
print("There is no five")
Copyright © 2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
3==4
Check if a value is exactly equal to the
"== other ouput: False
3!=4
Ascertain that a value is not equal to the
!= other ouput: True
import re
import re Import Python's built-in regular expressions re.findall("strings", variable)
import re
someText="Hello regular
expression"
a=re.findall("regular|Hello",
someText)
print(a)
Check if either of two elements are present
a|b in a string output: ['Hello', 'regular']
import re
someText="Hello regular
expression"
a=re.findall("expression$",
someText)
Check if a variable ends with a set of
string$ strings output: ['expression']
import re
someText="Hello regular
expression"
a=re.findall("^Hello", someText)
print(a)
Check if a variable starts with a set of
^string strings output: ['Hello']
a= "Hello World"
a.index('H')
Check the index position of a string
string.index() character output: 0
a= "Hello World"
a.capitalize()
Capitalize the first character in a set of
string.capitalize() strings output: 'Hello world'
a= "Hello World"
a.swapcase()
1+3
output:
x, y = y, x Swap variables 3 [1, 2]
a=[1, 3, 5]
for b in a:
print(b, "x", "2", "=", b*2)
output:
1x2=2
3x2=6
for Loop through the elements in a variable 5 x 2 = 10
a=4
b=2
while b<=a:
print(b, "is lesser than", a)
b+=1
output:
2 is lesser than 4
Keep looping through a variable, as far as a 3 is lesser than 4
while particular condition remains True 4 is lesser than 4
Copyright © 2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
x=range(4)
print(x)
range(0, 4)
for b in x:
print(b)
output:
0
1
Create a range of positive integers 2
range() between x and y 3
print(sum([1, 2, 3]))
output: (3, 5)
Pop out the last item from a dictionary and print(a)
dict.popitem() delete it output: {1:2, 4:8}
a=[1, 3, 2, 4, 1, 6, 6, 4]
a.pop(-2)
output: 6
Pop out a given index from a list and print(a)
list.pop() remove it from a list output: [1, 3, 2, 4, 1, 6, 4]
x=[1, 3, 5]
x.clear()
print(x)
Copyright © 2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com
Command Line
pip install package Install an online library pip install pandas
Use virtaulenv to create a virtual
virtualenv name environment virtualenv myproject
Use virtual environment wrapper to create
mkvirtualenv name virtual environment mkvirtualenv myproject
python file.py Run the commands in a Python file "python my_file.py
List out all the installed packages in a
pip freeze virtual environment pip freeze
pip freeze > somefiles Copy all installed libraries in a single file pip freeze > requirements.txt
where Find the installation path of Python where python
--version Check the version of a package python --version
.exe Run a Python shell python.exe
Write to an existing file and overwrite its with open('regex.txt', 'w') as wf:
with open(file, 'w') existing content wf.write("Hello World!")
with open('regex.txt', 'r') as rf:
with open(file, 'r') Open a file as read-only print(rf.read()
Write to a file without overwriting its with open('regex.txt', 'a') as af:
with open(file, 'a') existing content af.write("\nHello Yes!")
af=open('regex.txt')
file.close Close a file if it's not in use af.close
exit Exit the Python shell exit()
Copyright © 2020 MakeUseOf. For more cheat sheets, head over to www.makeuseof.com