Variables and Data Structures in Python

In other programming languages like C, C++, and Java, you will need to declare the type of variables, but in Python you don’t need to do that. Just type in the variable and when values will be given to it, it will automatically know whether the value given would be an int, float, or char or even a String.

Python program to declare variables

myNumber = 3
print(myNumber)

myNumber2 = 4.5
print(myNumber2)

myNumber ="helloworld"
print(myNumber)

Output:

3
4.5
helloworld

That's it! Just create a variable, assign it any value you want and then use the print() function to print it.

Python has 4 types of built in Data Structures: List, Dictionary, Tuple and Set.

List is the most basic Data Structure in python. List is a mutable data structure, i.e items can be added to list later after the list creation. It’s like you go shopping at the local market and made a list of some items; later on you can add more and more items to the list. The append() function is used to add data to the list.


# Python program to illustrate a list 

# creates a empty list
nums = [] 

# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")

print(nums)

Output:

[21, 40.5, String]

# is used for single line comments in Python. """ this is a comment """ is used for multi line comments