Table of Contents
In this article, I will take you through Python Variables, Data Types and Conditionals. Python like any other programming languages uses variables to store all kind of data values. But unlink other programming languages, here you don't have to declare a variable before using it. A variable gets created the moment you assign a value to it. Apart from variables, we will also look into different data types and Conditionals in Python with examples in below section.
Python: Variables, Data Types and Conditionals with Examples
Also Read: Step by Step Guide to Install and Setup Django Project in PyCharm
1. Creating a Variable in Python
A variable provides us with a way to label an access information. We create variables with the name of the variable on the left and the value we want to store on the right as shown below. This is called an assignment statement. Once we created our variable, we can reference it and get the value restored. This means that we will have less repeated calculations and more organized code.
>>> sum = 3
Using variable also makes our code more readable by assigning meaning to our values. This can be understood further using below example.
>>> sum = 3 >>> result = 5 + sum >>> result 8
In the above example, we are using two variables - sum and result. First we are storing a value 3 to the sum
variable, then we are adding up this value with 5 and storing the resultant into result
variable. Now we can reference this variable and get the values we calculated later.
2. How to Name the Variables
There are pretty simple rules to name the variable. Those includes:-
- No spaces in the name of the variable
- No Digits or Special Characters like 3,4 $, # in front.
- Use simple and meaningful names so that others can understand your code.
- Pep 8 Style Guide recommends to use lowercase characters for variables.
- Pep 8 Style Guide also recommends to use underscores for spaces.
3. Built In type() Function
Python has the capability to determine and show the data type of a variable based on the data it stored using a built In function called type()
. Once a value is assigned to the variable, you can check the data type of that variable by using like below.
>>> x = 8 >>> type(x) <class 'int'> >>> x = 'hello' >>> type(x) <class 'str'> >>> x = 4.7 >>> type(x) <class 'float'>
4. Mathematical Operators
Python interpreter can evaluate any mathematical expressions we type in and it supports basic operators like addition, subtraction, multiplication, division, exponent and negation.
Addition
>>> 3 + 5 8
Subtraction
>>> 10 - 5 5
Multiplication
>>> 3 * 5 15
Division
>>> 25 / 5 5.0
Exponent
>>> 3 ** 3 27
Negation
>>> -4 + -2 -6
5. Integers and Floats
Python has two types of numbers - Int
and Float
. An Int is a whole number and a float is a decimal number. Any time if you see a number without a decimal point, it is an int.
6. Order of Operations
In Python just like any other programming languages, order of operations is very important. This can be understood from few of the below given examples.
>>> (5 + 7) * 3 36
In the above expression, (5 + 7) is going to be evaluated first because of parentheses. Then behind the scene, we will have 12 * 3 with the final result of 36.
>>> 5 + 7 * 3 26
In the above expression, 7 * 3 is going to be evaluated first giving the result of 21. Then behind the scene, 5 + 21 will get evaluated with the final result of 26.
So now the important question that you might ask here is that how the order of operations is determined ? Simple, Python follows the rule of PEMDAS which means Parentheses Exponent Multiplication Division Addition Subtraction.
7. Built In print() Function
Python provides an excellent built in function called print()
to display its arguments as a line of text on the output. You can use either single or double quote to enclose the argument inside the function as shown in below example.
>>> print('Hello World !!') Hello World !! >>> print("Hello World !!") Hello World !!
8. Conversion Using str() and int() Function
Python provides some more useful functions like str()
and int()
which can be used to convert integer to string and vice versa. In the below example, we are first assigning integer value 10 to a variable num and then converting this integer to string using str(num)
function. Similarly, in the second example, we are first assigning a string value to variable x and then converting it to integer value using int(x)
.
>>> num = 10 >>> str(num) '10' >>> x = '10' >>> int(x) 10
9. Introduction to Conditionals
Conditional Statements are also called decision making Statements. This is used when we want to execute a block of code based on some condition. Check More about Operators and Expressions in Python.
a) Comparison Operators
There are basically six types of comparators in Python:-
- less than(<)
- less than equal to(<=)
- equal to(==)
- greater than equal to(>=)
- greater than(>)
- not equal to(!=)
b) Booleans
In programming, Boolean is a type which means a condition can either be true or false.
>>> sum = 3 >>> if sum < 4: ... print('Sum is Correct') ... Sum is Correct
10. User Input - Input() Function
Instead of using hard coded values in Python, you can actually ask user to provide the value using an In built function called Input()
as you can see below. This function will print out the statement to the user and then wait for the user to answer and then save that value.
>>> day = input('Enter the current day of the week: ') Enter the current day of the week: Monday