Skip to content

Learning Objectives

  • Understand the different data types (e.g. strings, integers, float, lists, tuples, dictionaries) and apply the use of the correct data type in different situations
  • Understand the rules and common conventions of naming variables in using Python
  • Understand how to store data of different data types in different variables
  • Use variables in the print function with f-strings

Data Types

Strings

  • Strings are surrounded by either single inverted (') or double inverted commas (")
  • Strings are commonly used to store textual and numeric data (with no calculations involved)

Here are some examples of strings:

python
"Hello"
"123"
"456"

Integers

  • Integers are not surrounded by either single inverted (') or double inverted commas (")
  • Integers are used to store postive and negative numbers without decimal points

Here are some examples of integers:

python
123
-456
259

Float

  • Float are similar to integers but it is possible to store floating (decimal) point numbers
  • Float can also store both positive and negative numbers

Here are some examples of float:

python
123.0
-456.0
888.888

List

  • List are used store multiple elements (item) in a single variable
  • List are ordered (we can access the different elements with a convention) and mutable (we can edit the data anytime)
  • List can contain elements of different datatypes

Here are some examples of a list:

python
['I', 'love', 'Python']
[995, 999, 991]
[1.28, 3.40, 9.00]

Tuples

  • Tuples are similar to lists but is immutable (we cannot edit the data once defined)

Here are some examples of a tuples:

python
('I', 'love', 'Python')
(995, 999, 991)
(1.28, 3.40, 9.00)

Dictionaries

  • Dictionaries store data in key (word), value (definitions) pairs similar to the real-world dictionary
  • Here are some examples of dictionaries:
python
{
    'A': 20,
    'B': 30,
    'C': 20
}

Type Function

We can also check the data type using the type() function:

python
print(type("Hello"))
print(type(1))
print(type(2.0))
print(type([999, 995, 2004]))
print(type((999, 995, 2004)))
print(type({
    'A': 20,
    'B': 30,
    'C': 50
}))

Output:

python
<class 'str'>
<class 'int'>
<class 'float'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

Variables

  • Variables are used to store and manage data that can be referenced and manipulated throughout a program
  • Don't Repeat Yourself (DRY) principle: Use variables to avoid repeating common items (e.g. your name, age) in your code

We can define variables in this way:

variable_name = data (in any data types)

Here are some examples:

python
name = "Tom"
age = 18
hobbies = ['coding', 'learning Python']

Rules for naming variables

  • A variable name can only start with (_ or a alphabet) cannot start with numbers
python
_time = 50
time = 20
  • You cannot use spaces in multi-word variable names but you can replace it with underscores (_):
python
my_hobbies = ['Basketball', 'Badminton', 'Cricket']
  • Create meaningful variable names:
python
age = 21

Printing with Variables (printf)

After defining our variables, we can use it together in the print() function followed by a format string:

  • Put a f before the terminating inverted commas
  • Wrap the variable names in curly braces before using them
python
name = "Tom"
age = 21
hobbies = ['basketball', 'badminton', 'cricket']
print(f"I am {name} of age {age} and I like {hobbies}")

Output:

I am Tom of age 21 and I like ['basketball', 'badminton', 'cricket']