Getting Started – Variables and Data types
Introduction
When working with Python, the best thing to do is to assign variables. A variable is a piece of data that holds a value that may change. You can use variables to store all types of data; numbers, text, whole tables or all the data of a file. The reason for doing this is to make your code simpler and more understandable. It also easier to re-use when you give a name to your data. Every variable in Python is an object.
In other programming languages, every variable has to be declared before it can be used. Declaring a variable means binding the variable to a data type, which is not required in Python.
Suppose we want to assign a variable of a car:
Brand | No. passenger | Price in USDk | Damaged |
Toyota | 4 | 17.55 | N |
String
‘Brand’ is a string. A string is a set of characters including numbers, letters, signs etc. Notice that the set of character we used are letters and therefore quotation marks are used.
Brand = ‘Toyota’
(you could also have used double quotation marks).
Integer
“Number of passengers” of 4 is an integer. There is no decimal
Passengers = 4
Float
‘Price in USDk’ in is a float i.e. they represent real numbers and are written with a decimal point dividing the integer and fractional parts.
Price = 17.55
Boolean
Boolean is data type used to represent logic values True and False. True generally used positively. False is generally used negatively. If the car is damaged then the variable is True.
Damaged = True
Strings
Press the ‘run’ button to see what Python prints out.
# print out the string03 using the “print” function
# Try with one qoute only
string01 = 'Toyota'
print(string01)
# it works also with two quotes
string02 = "Toyota"
print(string02)
# create a variable called string03 and assign it to the brand "Honda"
# feel free to try out a few more variable. Why not try out a variable with two words “Renault Clio”?
Integer and Float
Integers and Floats are the most common used numbers in Python. Python does support more types of number but we will focus on these two for now.
integer01 = 4
print(integer01)
float01 = 4.6
print(float01)
#convert integer to float
float02 = float(integer01)
print(float02)
Boolean
This datatype is on the face of it very simple. However, it is more used then you might realize. It is used a lot to trigger an action if the outcome of a statement is either True of False. Another word of caution is that people very easily forget to use double ‘=’ i.e. to test whether the variable is True or False.
In the console we create two variables:
x = 5
y = 6
To test this we press the ‘run’ button and two statements will be evaluated.
# create variables
x = 5
y = 6
# First statement: x is not equal to y
First_statement = x != y
print ("First_statement is " + str(First_statement))
# Second statement: x is greater than y
Second_statement = x > y
print ("Second_statement is " + str(Second_statement))
#Third statement: x is less than y
Third_statement = x < y
print ("Third_statement is " + str(Third_statement))
# Fourth statement: x is greater than or equal to y
Fourth_statement = x >= y
print ("Fourth_statement is " + str(Fourth_statement))
# Fifth statement: x is less than or equal to y
Fifth_statement = x <= y
print ("Fifth_statement is " + str(Fifth_statement))
# make a few more variables and try out some comparison operators