Professional Documents
Culture Documents
Declaration of variables
s = "This is a string"
a = 2
b = 2.4
type(b)
float
Integers
An integer is a number which is not a fraction; a whole number.
Examples of integers:
• -23
• -5
• 0
• 1
• 78
• 1,980,350
Examples of non-integers:
• 3.14
• 0.001
• 9,999.999
a = 2
b = 3
type(a), type(b)
(int, int)
type(a + b)
int
c = a + b
type(c)
int
Float
A float is a numbers with digits on both sides of a decimal point. This is in contrast to the
integer data type, which houses an integer or whole number.
Examples of floats:
• 3.13
• 9.99
• 0.0001
Note that a float data type in Python can represent a whole number.
• 1.0
• 3.0
a = 1.1
b = 2.3
type(a), type(b)
(float, float)
type(a + b)
float
a + b
3.4
i = 1
type(i)
int
type(i + a)
float
i + a
2.1
Math functions
• abs() Returns absolute value of a number
• pow() Raises a number to a power
• round() Rounds a floating-point value
abs(-2)
pow(2, 3)
2**3
round(3.14, 1)
3.1
f = 1.234456778
round(f, 4)
1.2345
Strings
A string in Python is a sequence of characters.
Examples
• "I am a string!"
• 'I am another string'
s1 = 'I am a string'
s2 = "I am another string"
s1 + s2
a = 1
s1 + a
Boolean
• True or False (case sensitive)
• Either it rains or not -> Either you take your umbrella or not
True
True
False
False
b = True
True
b = 10 > 5
True
bool(2 > 4)
False