You are on page 1of 2

1.

0 VARIABLES AND DATA TYPES

1.1 Variables

A variable is a container for storing data. In python, we can save data by assigning them variable

names. This is done by the assignment operator → =. The variable name goes to the left of the equal

sign, while the data we are assigning goes to the right of the equal sign. The variable name

represents a location in computer memory where, after the assignment statement executes, the data

will be stored. A simple assignment operation can be:

a=5

A = 10

1.2 Data types

Python has the following data types:

Numeric – int, float, complex

int: x = 4

float: y = 5.0

complex: z = 2 + 4i

Character or text – string

string: greet = “Hello World”

Sequence – list, tuple, range

list: fruits = [“guava”, “banana”, “orange”]

tuple: = (“guava”, “banana”, “orange”)

range: myRange = range(1,6)

Mapping types – dict

dict: myDictionary = {‘name’: ‘Alice’, ‘age’: ‘30’}

Boolean – bool

bool: isTrue = True


We can check the data type by using the “type()” function. We insert the name of the variable in to

the parenthesis. In addition, we can do an assignment of the various data types with or without the

constructor functions.

1.3 Input and Output

1.3.1 Input

The input() function takes input from a user.

user_age = input(‘enter your age: ’)

1.3.2 Output

The print() function allows results to displayed on the screen.

print(‘You are’, user_age, ‘old’)

Exercises

1. There are rules for naming python variables. What are they?

2. Asides from taking user input via typing, input can be gotten from an external file.

Investigate how this can be done and give examples.

You might also like