Python Variables and data types
Let's begin learning variables and data types in python programming. In this article we will learn about how to use variable to store the data. we will also learn about different data types provided by python programming language. In python, without using the variables and data types we can't able write programs that can solve problems.
What is a variable ?
Variable is a name which is used as a pointer to data that is stored on computer memory. In python we call variables as names.
message = "Hello World" print(message) # Output: Hello World my_var = "Hi" print(my_var) # Output: Hi print(your_var) # Output: # NameError: name 'your_var' is not defined
In above python program we have stored the message "Hello World" in the name/variable message. Actually it is stored in computer memory and variable "message" is a pointer to the data "Hello World". In above program when we print "your_var" we got an error. Because the the variable/name is not defined. Instead of naming the variable as "message" we can use anything like "my_var", "your_var", etc. But, it's not a good practice to give random names. We should follow some rule to name variables in python.
Note: Use variable/names only if you use it more than once
Variable naming rules in python
- variable name should not contain a space
- variable name should starts with a letter or an underscore(_)
- special symbols (~, !, @, #, $, etc.) are not allowed in variable name.
- variable should be short and meaningful as per the data stored in variable.
- If we want to use multiple words in a variable we can use underscore(_) to separate the words. Example variables "student_name", "account_number", "phone_num1", "phone_num2", etc.
- Python is case sensitive language so, it treats the lowercase letters and upper case letters differently. For example variable "Student" is different from the variable "student".
- We should not use python reserved variables/names as varibles though we are allowed to use it.
- It is better practice to use lowercase letters instead of uppercase letters.
Note: It needs some practice to use good variable names.
What is a datatype in python ?
Datatype is a classification of data which tells the computer that how the programer wants to use the data. For example the data "hello" is different from the data 255. data "hello" is of type string and 255 is of type int in python.
In python, we can classify the data types into two.
- Immutable data type
- Mutable data type
Immutable data type:
Immutable data type is defined as once we declare the data of a datatype then it will not allow us to modify it. The following data types are immutable data types in python.
- bool - Boolean data type
- int - Integer data type
- float - Float data type
- str - String data type
- tuple - Tuple data type
- frozenset - Immutable set data type
Mutable data type:
Mutable data typeis defined as it is a data type in which we can modify the data whenever we want. It's simply opposite to the immutable data type. The following data types are mutable data types in python.
- list - List data type or Array data type
- set - Set data type
- dict - Dictionary data type
In python we can also define our own data types. The above mentioned data types are some of the built-in data types provided by the python. We will learn more about each of them in coming articles.