Last active
February 4, 2019 15:48
-
-
Save xmodar/61a951009dd8f2c2205f4606b984f87a to your computer and use it in GitHub Desktop.
A cheat sheet for Python I made a while ago.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python Cheat Sheet | |
#Any thing preceded by a '#' is a comment and won't be executed like this line here | |
#Variables are names defined by the programmer that hold values | |
WonderfulVariableByMe = 5 | |
#There are some reserved words like: for, if, def, while, break, ... that cannot be used to name a variable | |
#A variable name must not contain spaces and must never start with a number | |
#Variable names are case-sensitive: modar does not equal Modar | |
#Variable values can be | |
# Single Values: | |
# 1- linear: | |
# An integer 5, 4, 0, -4, ... | |
# A floating point number 2.5, -2.6, 14.0, ... | |
# A character 'a', 'A', 'e', ... | |
# Think how can we represent a single quote character ' | |
# 2- boolean: | |
# Either (True or False) or (1 or 0) | |
# 3- string: | |
# A sequence of characters with the help of escape character you can represent any text | |
# \' single quote | |
# \" double quote | |
# \\ backslash | |
# \n new line | |
# \r carriage return | |
# \t tab | |
# Important: Visit [http://www.tutorialspoint.com/python/python_strings.htm] | |
# for more information on accessing values in strings and some common strings operations | |
print 'Modar\\genius' #prints: Modar\genius | |
print 'Modar is \'Genius\'' #prints: Modar is 'Genius' | |
print 'Modar \nis \nGenius' #prints in separate lines: Modar is Genius | |
# 4- function: forget about it now | |
# 5- class object: more details later | |
# Multiple Values: | |
# 6- list | |
# 7- tuple | |
# 8- dictionary | |
# Important: Visit [http://www.tutorialspoint.com/python/python_variable_types.htm] | |
# and only read python lists, python tuples, python dictionary | |
#Arithmetic operators | |
# The usual arithmetic we know: +, -, *, /, ... | |
x = 5 | |
y = 7 | |
z = x * y | |
print z #prints: 35 | |
#comparison operators | |
# Used to compare values and always return boolean (either True or False): | |
# == is equal | |
# != is not equal (sometimes we use <> instead of != although they have same meaning) | |
# < less than | |
# > greater than | |
# <= less than or equal | |
# >= greater than or equal | |
# in is value in list | |
# is are they the same | |
a = 1 | |
b = 2 | |
c = 1 | |
s = [1, 3, 4] | |
x = a == b #x = False | |
x = a != b #x = True | |
x = a is c #x = False (Although they have the same values but a is not the same as c) | |
x = b < a #x = False | |
x = a > 7 #x = False | |
x = a <= b #x = True | |
x = a >= c #x = True (since they are equal) | |
x = a in s #x = True (since 1 is an element in s) | |
#combining boolean expressions | |
# and | |
# or | |
# not | |
x = (a < b) and (b < c) #x = False because one of the conditions is false | |
x = (a < b) or (b < c) #x = True because one of the conditions is true | |
x = not (a < b) #x = False because (a < b) is True; and not True is False | |
#Decision Making: | |
# using if condition | |
if a < b: | |
print 'Yes, a is less than b' | |
# Note: the colons ':' and the indentation before print | |
if a > b: | |
print 'Yes, a is less than b' | |
print 'Modar' | |
# Modar will be printed regardless of whether the condition is satisfied or not | |
# because it is not part of the condition and we know that by the indentation | |
# You can also nest conditions as you want: | |
if a < b: | |
print 'Yes, a is less than b' | |
if b > 10: | |
print 'And b is greater than 10' | |
# The full syntax of if conditions | |
if a == b: | |
print 'a equal b' | |
elif a < b: | |
print 'a is less than b' | |
else: | |
print 'a is greater than b' | |
# elif means else if which means if all above conditions were not satisfied | |
# You can add as many elif as you want | |
if a < b: | |
print 'ddd' | |
elif b > c: | |
print 'eee' | |
elif c > a: | |
print 'hhh' | |
#Loops: you can find tutorial on loops at the end of this document | |
# while loop | |
while a < 10: | |
print 'a is still less than 10' | |
a = a + 1 | |
print 'now a is equal to 10' | |
# for loop | |
myList = [5, 7, 6, 3] | |
# to print all elements of myList | |
for a in myList: | |
print a | |
# to reach elements in the list | |
for i in range(len(myList)): | |
myList[i] = myList[i] + 1 | |
print myList | |
#You can also nest loops | |
lol = [[1,2,3],[4,5,6],[7,8,9]] | |
for x in lol: | |
for y in x: | |
print y | |
#this will print all elements of all lists in lol | |
#Functions | |
# functions are piece of code that take input(s) and return output | |
# This function takes any number and returns the next number | |
def func(input): | |
a = input + 1 | |
return a | |
# to use it | |
n = 5 | |
next = func(n) | |
print next | |
# it can take many input | |
def add(x, y): | |
a = x + y | |
return a | |
print add(1, 2) #prints: 3 | |
print add(10, 5) #prints: 15 | |
#or it can take no input | |
def prnt(): | |
print 'function that takes no input and return nothing' | |
prnt() #this will print the sentence above | |
#Classes | |
# You don't have to concern yourself too much with classes | |
# All what you need to know is that you can think of them as libraries of function | |
# E.g. the class named math has a lot of functions like sin and cos | |
# [this is not 100% accurate but you can think of it like that] | |
import math | |
x = math.sin(maht.pi/2) # x = 1 because sin(pi/2) is one | |
#---------------------------------------------------------------------------------------------------------- | |
#Loops Tutorial | |
# The most basic loop is while loop | |
while a != 10: | |
print 'a is still not equal to 10' | |
a = a + 1 | |
print 'now a is equal to 10' | |
# while loop will keep looping as long as the condition is satisfied | |
# some wile loop will loop forever because the condition can never be satisfied | |
while a == 0: | |
print 'a is still not equal to 0' | |
# It is a common thing that we sometimes need to go through all elements of a list | |
myList = [5, 2, 6, 1, 8] | |
listLength = len(myList) | |
i = 0 | |
while i < listLength: | |
a = myList[i] | |
print a | |
i = i + 1 | |
# The previous code took 6 lines of code and this become tedious if because we need that a lot | |
# sometimes we need to go through all elements but we also need to modify them | |
i = 0 | |
while i < listLength: | |
a = myList[i] | |
a = a + 1 | |
i = i + 1 | |
print myList | |
# this will print the old list as is and will not change any value | |
# because (a) takes a copy of the element and not the element itself | |
i = 0 | |
while i < listLength: | |
myList[i] = myList[i] + 1 | |
i = i + 1 | |
print myList #prints: [6, 3, 7, 2, 9] | |
# Since working with list is tiresome, the introduction of a new kind of loop helped us a lot; for loop | |
for a in myList: | |
print a | |
# and that is it only two lines to print the content of the list | |
# to modify the list we cannot do the following | |
for a in myList: | |
a = a + 1 | |
print myList | |
# because a is a copy and not the element itself. Instead, we do this | |
r = [0, 1, 2, 3, 4] | |
for i in r: | |
myList[i] = myList[i] + 1 | |
print myList | |
# this is better but also tiresome because you need additional list from 0 up to the length of your list | |
# so python has provided us with a really nice function called range | |
r = range(5) # r = [0, 1, 2, 3, 4] | |
for i in r: | |
myList[i] = myList[i] + 1 | |
print myList | |
# or even better | |
for i in range(listLength): | |
myList[i] = myList[i] + 1 | |
print myList | |
#---------------------------------------------------------------------------------------------------------- | |
#Extras | |
# List comprehension [Set Notation] | |
# you can loop through all the elements of a list and apply some operations on them to construct a new list | |
myList = [2,1,5,6,8,9,4] | |
incrementedList = [element+1 for element in myList] # [3,2,6,7,9,10,5] | |
# you can also apply conditions and do a lot of stuff with it like thresholding | |
thresholdedList = [element for element in myList if element>0 and element<5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment