Python Revision Tour -
introduction
Tokens in Python
1. Keywords
predefined words with special meaning to language compiler or interpreter
cant be used as a normal identifier names
example: False, None, True, and, as, break, elif, if, etc.
2. Identifiers
Names given to different parts of the program viz. variables, objects,
classes, functions, lists, dictionaries.
Rules for Python identifiers
non-keyword word with no spaces in between
made up of only letters, numbers and underscore (_); no symbols
cannot begin with a number
3. Literals/values
String literals:
a sequence of characters surrounded by quotes
can be a multi-line string or single-line string
Numeric literals:
Integer/ int
decimal form: 19, eg, 1234, 6969, 420
octal form: beginning with 0o, 17, eg, 0o35, 0o75
hexadecimal form: beginning with 0x, 19, AF, eg, 079, 0xAF
Float
real numbers with a decimal point
eg, 1.0, 25.0, 203.34
Python Revision Tour - introduction 1
Complex numbers
form a+bi; where i= 1^1/2
Boolean literals
used to represent two values- True or False
Special Literal None
python has one special literal, None
used to indicate the absence of value
4. Operators
tokens which trigger some computation/action when applied to objects
eg. +,<,>,-,/,%, in, not in, =, in, is not, etc.
5. Punctuators
symbols used to organize sentence structures
most common: ' " # / ( ) { } @ : = .
Math Library functions
import math
math.<function name>
(OR)
from math import *
Some math functions
Description
Function
ceil(x) Returns the smallest integer greater than or equal to x.
fabs(x) Returns the absolute value of x
factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
isfinite(x) Returns True if x is neither infinity nor a NaN Not a Number)
isinf(x) Returns True if x is a positive or negative infinity
Python Revision Tour - introduction 2
Description
Function
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x - 1
log(x[,
Returns the logarithm of x to the base (defaults to e)
base])
log1p(x) Returns the natural logarithm of 1+x
log2(x) Returns the base-2 logarithm of x
log10(x) Returns the base-10 logarithm of x
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
acos(x) Returns the arc cosine of x
asin(x) Returns the arc sine of x
atan(x) Returns the arctangent of x
atan2(y, x) Returns atan(y / x)
cos(x) Returns the cosine of x
hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
degrees(x) Converts angle x from radians to degrees
radians(x) Converts angle x from degrees to radians
acosh(x) Returns the inverse hyperbolic cosine of x
asinh(x) Returns the inverse hyperbolic sine of x
atanh(x) Returns the inverse hyperbolic tangent of x
cosh(x) Returns the hyperbolic cosine of x
sinh(x) Returns the hyperbolic cosine of x
tanh(x) Returns the hyperbolic tangent of x
erf(x) Returns the error function at x
erfc(x) Returns the complementary error function at x
gamma(x) Returns the Gamma function at x
Returns the natural logarithm of the absolute value of the Gamma function
lgamma(x)
at x
Python Revision Tour - introduction 3
Description
Function
Mathematical constant, the ratio of the circumference of a circle to its a
pi
diameter 3.14159...)
e mathematical constant e 2.71828...)
Random module
This module implements pseudo-random number generators for various
distributions
import random
random.<function name>
(OR)
from random import *
Some important random functions
Code Notes
Functions
Untitled
Return a randomly selected
Randrange() random.randrange(start, stop[, step]) element from range(start, stop-
1, step)
Return a random integer N such
Randint() random.randint(a, b) that a <= N b. Alias
for randrange(a, b+1.
Return a random element from the
Choice() random.choice(seq) non-empty sequence seq. If seq is
empty, raises IndexError
Return the next random floating
Random() random.random() point number in the range 0.0,
1.0.
link to more random functions: https://docs.python.org/3/library/random.html
Type Conversion
Type Casting/ Explicit type conversion
User-definedtype conversion and thus known as Explicit type conversion
Python Revision Tour - introduction 4
for example,
a=3 ; b=5.0
>>>int(b)
>>>b=5
Type Promotion/ Implicit type conversion
Python automatically converts one data type to another data type. This
process doesn't need any user involvement
for example,
a=3 ; b=5.0
>>>c=a+b
>>>c=8.0 (float)
Looping Statements
3.1 The break statement
terminates the very loop it lies within
skips the rest of the loop and jumps over to the statement following the
loop.
3.2 The continue statement
the continue statement forces the next iteration of the loop to take place,
skipping any code between
Python Revision Tour - introduction 5