MyMisPal
← Python lessons
Lesson 2 of 13

Variables and data types

A variable is a name that refers to a value in memory. You create it with =, which means “store”, not “equals”.

PYTHON
name = "Sara"
age = 21
gpa = 4.5
is_mis = True
print(name, age, gpa, is_mis)
print(type(age), type(gpa), type(name), type(is_mis))
whole numberint → 21, -5, 0
decimal numberfloat → 4.5, 0.15
textstr → "Sara", 'IT'
true / falsebool → True, False

Sometimes a value is text but you need a number, like data read from a file. Convert with int(), float() and str().

PYTHON
price = "4800"
qty = 3
total = int(price) * qty
print(total)
print("Total: " + str(total))
Watch outVariable names use letters, digits and _, and can't start with a digit. Python convention is lowercase with underscores, like total_price.

Exercises

Exercise 1

Create product = "Laptop" and price = 4800, then print them on one line: Laptop 4800.

Exercise 2

You have text = "250". Convert it to a number and print it multiplied by 4.

Spotted a mistake on this page, or have an idea to improve the site?