You can store and manipulate all different types of data: a number, a string, list, and so on. With Python, you don't need to declare all your variables before you use them.
Variables
A variable is a labeled location in memory that is used to store values within a computer program. There are two types of variables: local and global.
Local Variables
Global Variables
Global variables are defined in the main body of the program outside any particular functions. These variables can be seen by any function and are said to have global scope.
Here in the example below, “a” and “b” are global variables, while “firstnum,” “secondnum,” and “sum” are local variables.
You would not be able to access the variables “firstnum,” “secondnum,” and “sum” from outside the “addnum” function.
Basic Data Types
A variable can store various types of data, called a data type . Let’s introduce some of the basic types we'll be looking at.
Integers
Floating Point Numbers
Strings
Lists
A list is an ordered sequence of data items usually of the same type, each identified by an index (shown in the circles). This is known as a one-dimensional list.
You would end up with something like this:
Let's look at a program. Open the file list1.py. Here, we've created a list and initialized it with some data.
Two-Dimensional Lists
Two-dimensional lists are visualized as a grid similar to a table with rows and columns. Each column in the grid is numbered starting with 0. Similarly, each row is numbered starting with 0. Each cell in the grid is identified by an index – the row index followed by the column index (shown in the circles).
Let’s take a look at a program. Open the file list2d.py. Here, we've declared our shoreSheet list and initialized it with some data .
Sets
A set is an unordered collection of unique items enclosed in curly braces { }. Sets can contain different types.
Let’s take a look at a program. Open the file sets.py. Here, we've created a set with some animal names. We can output the data in the set.
We can also add an item to the set using the .add() method .
Tuples
A tuple is similar to a list and is a sequence of items each identified by an index. As with lists, the index starts with 0 not 1.
In contrast to lists, items in a tuple can’t be changed once assigned, and tuples can contain different types of data.
Use a tuple when you want to store a data of a different type, such a sign in details for your website.
Let’s take a look at a program. Open the file tuple.py. Here, we've created a tuple with some colors.
Dictionaries
A dictionary is an unordered collection of items, each identified by a key.
Let’s take a look at a program. Open the file dictionary.py. Here, we've created a dictionary with some user data .
Program Input
One of the main reasons for writing a program is so you can run it multiple times with various different data.
Instead of hard coding the input data into a variable as we’ve done previously, it would be better to prompt the user for the input or retrieve it from a file (see later).
Program Output
Casting Data Types
Variables can contain various types of data such as text (called a string), a whole number (called an integer), or a floating point number (numbers with decimal points).
With Python, you don’t have to declare all your variables before you use them. However, you might need to convert variables to different types. This is known as type casting .
Python has two types of type conversion: implicit and explicit.
With implicit type conversion, Python automatically converts one data type to another.
With explicit type conversion, the programmer converts the data type to the required data type using a specific function. You can use the following functions to cast your data types:
int( ) converts data to an integer
long( ) converts data to a long integer
float( ) converts data to a floating point number
str( ) converts data to a string
This example would prompt the user for some data and then store the data in the “a” variable as a string.
Arithmetic Operators
Within the Python language, there are some arithmetic operators you can use.
Operator Precedence
- 1.
Brackets ( )
- 2.
Indices (sqrt, power, squared2, cubed3, etc.) **
- 3.
Divide /
- 4.
Multiply *
- 5.
Add +
- 6.
Subtract -
Performing Arithmetic
Comparison Operators
These are used to compare values and are commonly used in conditional statements or constructing loops.
Boolean Operators
Also known as logical operators and are commonly used in conditional statements (if...) or constructing loops (while... for...).
Bitwise Operators
Bitwise operators are used to compare binary numbers:
Lab Exercises
Write a program that accepts a length in inches and prints the length in centimeters (1 inch = 2.54cm).
Write a program that accepts your forename, surname, and year of birth and adds them to an array.
Write a program to calculate and display an employee’s gross and net pay. In this scenario, tax is deducted from the gross pay at a rate of 20% to give the net pay.
Write a program that stores a shopping list of ten items. Print the whole list to the screen, and then print items 2 and 8.
Extend the previous program, to insert an item into the list.
What is a Boolean operator? Write a program to demonstrate.
What is a comparison operator? Write a program to demonstrate.
What is data type casting? Why do we need it? Write a program to demonstrate.
Summary
A variable is a labeled location in memory that is used to store values within a computer program
Variables defined within a function are called local variables, as they are local to that particular function.
Global variables are defined in the main body of the program outside any particular functions.
An integer is a whole number and can be positive or negative. Integers can usually be of unlimited length.
A floating point number, sometimes called a real number, is a number that has a decimal point.
A string must be enclosed in quotes.
A list is an ordered sequence of data items usually of the same type, each identified by an index.
A set is an unordered collection of unique items enclosed in curly braces.
A tuple is similar to a list and is a sequence of items each identified by an index. Items in a tuple can’t be changed once assigned and tuples can contain different types of data.
A dictionary is an unordered collection of items, each identified by a key.
We can use the input() function to prompt the user for a number.
Converting variables to different types is known as type casting.