




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python Revision notes containing order of operations, variables, integers, string, conditional statements( if, if else, if elif else), string methods, string slicing and indexing.
Typology: Cheat Sheet
1 / 8
This page cannot be seen from the preview
Don't miss anything!





HELP from Documentation
dir(module) help()
Important Characters and Sets of Characters
tab \t new line \n backslash \ string (^) " " or ' ' docstring """^ """ comparison operators == , < , > , <= , >= , != Python type boolean True , False. Logical operators not , and , or
Order of Operations
Operator Description () (^) Parentheses (grouping) f (args...) (^) Function call x [index:index] (^) Slicing x [index] (^) Subscription x.attribute (^) Attribute reference ** (^) Exponentiation
Comparisons, membership, identity
not x (^) Boolean NOT and Boolean AND or (^) Boolean OR
Variable Names
Six Steps to Defining a Function
Integers and Strings
int(45) 45 int('45') 45 str(45) '45' str('45') '45' int(str(45)) 45
Calling Methods
module_name.function_name(x)
o object.method(ar guments) o dir(str) o help(str.method)
for Loops
num_vowels = 0 for char in s: if char in 'aeiouAEIOU': num_vowels = num_vowels + 1 print num_vowels vowels = '' for char in s: if char in 'aeiouAEIOU': vowels = vowels + char print vowels
Lists
Like for strings, slicing and indexing can also be used for lists
List = ['a','b',1]
length of list len(list) smallest element in list min(list) largest element in list max(list) sum of elements of list (where list items must be numeric) sum(list)
a=[1,'ab',2,'pq'] a[1][0] 'a' a[1][1] 'b' a[3][1] 'q' a[3][2]
List Methods
a = [5] + [6] + ['a',7] print (a) [5, 6, 'a', 7]
List Mutability
We say that lists are mutable: they can be modified.
lst = [1, 2, 3] lst[0] = 'apple' lst ['appple, 2, 3]
List Aliasing
Consider the following code:
lst1 = [11, 12, 13, 14, 15, 16, 17] lst2 = lst lst1[-1] = 18 lst [11, 12, 13, 14, 15, 16, 18]
After the second statement executes, lst1 and lst2 both refer to the same list. When two
variables refer to the same objects, they are aliases. If that list is modified, both of lst1 and lst will see the change.
But be careful about:
lst1 = [11, 12, 13, 14, 15, 16, 17] lst2 = lst
def secret(s): i = 0 result = '' while s[i].isdigit(): result = result + s[i] i = i + 1 print result
secret('123') will give an error message when it runs the fourth time.
Global and Local Variables
Variables defined outside functions are global variables. Their values may be accessed inside
functions without declaration.
To modify to a global variable inside a function, the variable must be declared inside the function
using the keyword global.
def x(): global num num = 5 def y(): num = 4
num = 7 print (num) a[3][1] 'q' >>> a[3][2] List Methods - append a value or string list.append('a') - extended by another list list.extend(['a', 'b']) >>> a = [5] + [6] + ['a',7] >>> print (a) [5, 6, 'a', 7] List Mutability We say that lists are mutable: they can be modified. >>> lst = [1, 2, 3] >>> lst[0] = 'apple' >>> lst ['appple, 2, 3] List Aliasing Consider the following code: >>> lst1 = [11, 12, 13, 14, 15, 16, 17] >>> lst2 = lst >>> lst1[-1] = 18 >>> lst [11, 12, 13, 14, 15, 16, 18] After the second statement executes, lst1 and lst2 both refer to the same list. When two variables refer to the same objects, they are aliases. If that list is modified, both of lst1 and lst will see the change. But be careful about: >>> lst1 = [11, 12, 13, 14, 15, 16, 17] >>> lst2 = lst def secret(s): i = 0 result = '' while s[i].isdigit(): result = result + s[i] i = i + 1 print result >>> secret('123') will give an error message when it runs the fourth time. Global and Local Variables Variables defined outside functions are global variables. Their values may be accessed inside functions without declaration. To modify to a global variable inside a function, the variable must be declared inside the function using the keyword global. def x(): global num num = 5 def y(): num = 4 >>> num = 7 >>> print (num) 7 x() print (num) 5 y() print (num) 5
Dictionaries
The values of a dictionary can be of any type, but the keys must be of an immutable data type such
as strings, numbers, or tuples.
keys can be numbers, strings, Booleans o a list is unhashable in a dictionary (cannot be used as a key) o a tuple is hashable in a dictionary (can be used as a key). values can be dicts, strings, numbers, booleans, lists
for key in my_dict: value = my_dict[key]
This is same as:
for key, value in my_dict.items():