ENG1090 Study Guide - Final Guide: Delimiter, Increment And Decrement Operators, Floating Point

81 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
Ptho, oe\to
list
Variable sequence
[1,to,3.5]
Method
Description
Example
Output
it→
Converts to integer 34
prit
34
it.→
Truncates the fractional part
prit.)
34
itf,→
Second parameter is the base
and it returns the number in
base 10
print(int('3f',16))
63
float-.e→-1128000000.0
Converts to float
print(float('-11.28e8'))
-1128000000.0
roud.,→.
Rounds to 1 decimal
print(round(56.67,1))
56.7
hr→a
Returns the char of the code

print(chr(97))
a
orda→
‘eturs the ode of har a
prit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]
[,apple]
[,apple,.]
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)
Nae= Ptho
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, leo, orage, grape]
Positive Index 0 1 2 3
Negative Index -4 -3 -2 -1
List[:-1] results in [apple, leo, orage]
List[1: ] results in [leo, orage, grape]
List[ : ] results in [apple, leo, orage, grape]
len(List) = 4
find more resources at oneclass.com
find more resources at oneclass.com
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