You are on page 1of 4

Variables

• A variable is a reserved memory location.


• A variable in a python program gives data to the computer for processing.

Basic data types


• Integers
• Floats
• Strings
• Boolean

Python Variable Name Convention


A variable name in Python should fulfil the following
• Use lowercase
• Separate words with underscore.
A variable name in Python can be
• A single lowercase letter
• A lowercase word
• Or lowercase words separated by underscore.
Examples of variable names
• x
• i
• cars
• my_cars
Note that other programming languages can have other naming conventions.

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

'I am a stringI am another string'

a = 1

s1 + a

print(f"I am a string with an int here {a + 2} - and more text")

I am a string with an int here 3 - and more text

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

You might also like