FIT1045 Study Guide - Final Guide: Substring, Delimiter, Increment And Decrement Operators

258 views3 pages
Python cheat sheet
Type
Description
Example
int
32 bit integer
2, -3
float
Floating point numbers
3.6, -5.98, 3.45e100
bool
Boolean
True, False
str
Character sequence
“Python”, “one\ntwo”
list
Variable sequence
[1,’two’,3.5]
Method
Description
Example
Output
int(“34”)→34
Converts to integer 34
print(“34”))
34
int(“34.66”)→34
Truncates the fractional part
print(“34.66”)
34
int(“3f”,16)→63
Second parameter is the base
and it returns the number in
base 10
print(int('3f',16))
63
float(“-11.28e8”)→-1128000000.0
Converts to float
print(float('-11.28e8'))
-1128000000.0
round(56.67,1)→56.7
Rounds to 1 decimal
print(round(56.67,1))
56.7
chr(97)→a
Returns the char of the code
‘97’
print(chr(97))
a
ord(“a)→97
Returns the code of char “a”
print(ord(“a”))
97
Method
Example
output
L.append(obj)
L=[]
Print(L)
L.append(1)
print(L)
L.append("apple")
print(L)
L.append(2.3)
print(L)
[]
[1]
[1,’apple’]
[1,’apple’,2.3]
L.pop([index])
L=[1,4,3,7]
x=L.pop()
print(x)
y=L.pop(1)
print(y)
print(L)
7
4
[1,3]
L.remove(obj)
L=[1,4,3,1,5]
L.remove(1)
print(L)
[4,3,1,5]
L.reverse()
L=[1,2,3,4,5]
L.reverse()
print(L)
[5,4,3,2,1]
L.sort()
L=[3,2,1,5,6]
L.sort()
print(L)
[1,2,3,5,6]
Situation
Example
Output
Assign a single value to a variable
x=2
print(x)
Name= “Python”
print(Name)
2
Python
Assign a formula or mathematical
operation to a variable
x=2+3
print(x)
5
Assign to multiple variables same value
x=y=z=0
print(x,y,z)
0 0 0
Assign different values to multiple variables
or doing multiple assignments
x,y,z=1,2,3
print(x,y,z)
1 2 3
Swapping values
a=2
b=3
a,b=b,a
print(a,b)
3 2
Increment the value of a variable by 1
x=1
x+=1
print(x)
2
Decrement the value of a variable by 1
x=1
x-=1
print(x)
0
Data Types
Variable assignment (=)
Type Conversion
List Operations
Lists
Lists are containers for holding values
Ex : if List is [‘apple’, ‘lemon’, ‘orange’, ‘grape’]
Positive Index 0 1 2 3
Negative Index -4 -3 -2 -1
List[:-1] results in [‘apple’, ‘lemon’, ‘orange’]
List[1: ] results in [‘lemon’, ‘orange’, ‘grape’]
List[ : ] results in [‘apple’, ‘lemon’, ‘orange’, ‘grape’]
len(List) = 4
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in