 Introduction to Python : Variables and basic data types

# Variables and basic data types

Variables are containers reserved in the memory of the computer to store values in a computer program. Example the expression

``grade = 89``

assigns the integer value 89 to the variable named 'grade'. So within the scope or lifetime of the variable, it will always refer to or contain the value 89.

The type of data a variable stores is inferred in Python at runtime. This phenomenon is called dynamic as opposed to static typing whereby data types are determined at compile time.

The basic data types that may be dynamically inferred at runtime in Python are:

Integers

An integer is a number that may be zero, positive or negative without a fractional part. So integers are a set of numbers consisting of zero (0), positive whole numbers (1, 2, 3, ...) and their inverse or opposite counterparts (-1, -2, -3, ...).

``````x = 10
y = -5
sum = x + y``````

in the program above, x, y and sum are all inferred as integer variables since they all store integers.

Float-point numbers

In simple terms, a floating point number is a number that contains a decimal point. For example 3 divided by 2 cannot be represented by a whole number but by a decimal number 1.5.

``````x = 10
y = 2.5
sum = x + y``````

In the program above, x and y are inferred as integer and float respectively and the sum of the two numbers which is 12.5 is inferred as a float.

Strings

A string is a series of characters stored as one unit. Strings are arrays in essence which can be declared by placing characters in quotes. Example:

``````first_name = "Justice"
full_name = "Justice McVeey"``````

The expressions above will declare two variables storing 'Justice' and 'Justice McVeey'

Boolean

A variable of boolean type may have one of two possible values: True or False. A boolean expression expresses truthfulness or falsehood. Boolean values are the results of comparison and logical operators. ##### Introduction to Python
Number of sections:
11
Content length:
Delivery formats:
Introduction to Python