Python Variables and Data Types

In previous tutorial you have learned about how to write and run your first Python program to print Hello World. This tutorial will help you to understand about Python variables and data types.

 

Python Variables

Variables are name of memory locations to store values. Variables are easy to use and work within Python environment. You just need to type the name of any variable without any datatype declaration and the Python environment will automatically get to know what datatype value the variables has.

Example

 

Output

5

 

The declaration of datatype is optional in Python. Here, we have directly assigned the value ‘5’ to the variable ‘a’ which takes it as an integer. In Python, we can use multiple assignment at one single point of time.

 

Output

(5, 5, 5)

 

Here are some other workarounds with print command in Python.

To include apostrophe in a print command, we can do it with the following method.

 

Output

Hello World by ‘thecrazyprogrammer.com’

 

You can use apostrophe(‘) within a print statement but it needs to be within double quotation marks in case you need to use apostrophe within the String.

print(‘Hello World ‘thecrazyprogrammer.com”) is not a valid statement and will generate error.

 

To use the print command with Strings (statements) on multiple lines

 

Output

Hello World
thecrazyprogrammer.com

 

This print command uses a line continuation character i.e., a backslash (\) which allows us to write strings on the next lines.

 

To repeat a String.

 

Output

the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer the crazy programmer 

 

Here, the multiplication operator or the asterisk acts as a repetition operator which multiplies and displays the string.

 

Python Data Types

Python has some standard data types defined in its library content. These are mentioned below.

 

Dictionary

Python provides us to form dictionaries with the use of Hash Table concepts. These are similar to associative types or hashes and makes use of keys and values associated to them. Dictionaries are declared with braces {} and values or data can be accessed using brackets [].

 

Output

dictionary1[‘Name’]: Tushar Soni
dictionary1[‘Age’]: 20
dictionary1[‘Class’]: MCA

 

String

Python provides us to store Strings in the programs similar to String of characters in C. It can be implemented as below.

 

Output

thecrazyprogrammer

 

String Concatenation

You can merge or concatenate two different strings into a single string in Python. This functionality can be achieved by using the + operator.

Example

 

Output

Hello World

 

Tuple

A tuple is a sequence data type in Python. It is very much similar to lists in Python. Tuples consists of data separated by a comma. However, the difference between lists and tuples is that tuples use parentheses () whereas lists use brackets []. Also, the data in the Tuples once defined cannot be altered. But, in case of lists, data can be changed or altered later on after declaration and definition. Implementation of Tuples in Python is as follows:

 

Output

(‘the’, 4.3, ‘crazy’, 3432, ‘programmer’)

 

Number

Number data type is used to store numeric data values in Python. It includes the following domain of values.

int: It represents integer values. It generally stores signed integer values.

float: It represents floating point values, generally real values with decimals.

complex: It can store complex numbers in python variables.

long: It can store long integers which can in turn store hexadecimal and octal values.

 

List

List in Python provides us with a structure to store multiple items of different data types. A list is defined within square brackets with single quotation marks or apostrophes. It can be used in programming as follows:

 

Output

[‘the’, 4.3, ‘crazy’, 3432, ‘programmer’]

 

If you found anything incorrect or missing in above Python variables and data types tutorial then mention it by commenting below.

2 thoughts on “Python Variables and Data Types”

  1. i dont get the difference between a list and tuple. If you use parentheses() in lists, the result is the same. so why the difference?

Leave a Comment

Your email address will not be published. Required fields are marked *