Professional Documents
Culture Documents
Section A
( ) Parentheses left-to-right
** Exponent right-to-left
* / % Multiplication / Division / Modulus left-to-right
+ – Addition / Subtraction left-to-right
= += -= *=
Assignment and Shorthand right-to-left
/= %= //= **=
Mixed Operations and Type Conversions
Mixed mode operations
Operation between int and float will yield float
Operation between int and complex will yield complex
Operation between float & complex will yield complex
One numeric type can be converted another using built-
in functions int( ), float( ), complex( ), and
bool( )
int( )removes the decimal portion from the quotient
Type Conversions
int (float, numeric string)
int (numeric string, base)
float (int, numeric string)
float (int)
complex (int, float).
complex (int/float, int/float)
bool (int / float)
str (int / float)
chr (int)
Built-in Functions
abs (x)
pow (x, y)
min (x1, x2, x3, ….)
max (x1, x2, x3, ….)
divmod (x, y) returns a pair (x // y, x % y)
round (x, [,n])
bin (x), oct (x), hex (x)
Built-in Modules
Python has many Built-in Modules. Each module
contains many functions
For sophisticated mathematical operations, modules
like math, cmath, random, and decimal can be used
math – many useful mathematical functions
cmath – functions for operations on complex numbers
random – functions for random number generation
decimal – functions for precise arithmetic operations
Mathematical Functions
sqrt (x)
factorial (x)
fabs (x)
log (x)
log10(x)
exp (x)
trunc (x), round (x)
ceil (x), floor (x), modf (x)
Trigonometric functions
degrees (x)
radians (x)
sin (x), cos (x), tan (x)
sinh (x), cosh (x), tanh (x)
asin (x), acos (x), atan (x)
hypot (x, y)
random (x), randint (x)
seed( ), seed (x)
Container Types
Container typically refer to multiple values stored together
list is an indexed collection of similar or dissimilar entries
tuple is an immutable collection
set is a collection of unique values
dict is a collection of key value pairs, with unique key
enclosed in ‘ ‘
Values in a list or tuple can be accessed using their
position
Python Types Jargon
Collection – a generic term for container types
Iterable – collection that can be iterated over using a loop
Ordered collection – elements are stored in the same
order in which they are inserted and can be accessed using
an index; generic term is Sequence
Unordered collection – elements are not stored in the
same order in which they are inserted and cannot be
accessed using an index
Immutable – unchangeable collection
Mutable – changeable collection
Python Types Jargon
String – ordered collection, immutable, iterable
List – ordered collection, mutable, iterable
Tuple - ordered collection, immutable, iterable
Set – unordered collection, mutable, iterable
Dictionary – unordered collection, mutable, iterable
Class and Objects
Every type is a class – int, float, complex, bool,
str, list, tuple, set, dict are all ready-made
classes.
User defined classes can also be created
Object is created from a Class, Object is an instance of a Class
A Class describes two things –
the form an object created from it will take
methods (functions) that can be used to access and manipulate
the object
Multiple objects can be created form one class
Comments and Indentation
Comments begin with #
Multi line comments must be enclosed between a pair of
‘’’ or ‘’’’’’
Indentation matters and should not be used casually.
Unexpected indent error will be reported.
If statements are long, they can be written in multi-lines
with each line except the last ending with /
Multi-line statements within [ ], { }, or ( ) don’t need /