This is our second tutorial in Python, in this tutorial we are going to learn How to Create Variables in Python, also you can watch the complete video training for this article.
You can check Python GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
Also you can watch the complete Video Tutorial for this article.
What are Variables ?
Variables are containers or placeholders for storing values. creating of variables are so easy in Python, you can just write the name and also the value of the variable. you don’t need to explicitly write the type of the variable. for example if your familiar with other programming languages like C++ or Java, when you write your variables, you need to specify the data type of the variables, but in Python you can just write the name and value of the variable.
OK now let’s create our variable like this.
1 2 |
number = 6 print(number) |
You can see, that we have created variable at name of number and i have stored the 6 value in my number variable. now i can use this number and print that in the screen. also we have not explicitly specified the data type of the variable.
1 2 3 |
6 Process finished with exit code 0 |
You can create variables for different data types, like string, float, text like this.
1 2 3 4 5 6 7 8 |
name = "Parwiz" print(name) text = "Welcome to codeloop.org" print(text) floatNumber = 3.4 print(floatNumber) |
There are different rules for variable names.
1: When you are going to create variable, so the name should start with a letter, or underscore character.
1 2 3 4 5 6 |
#allowed name_1 = "Parwiz" #not allowed 1_name = "Parwiz" |
So you can see in the code the first example is allowed, but the second example is not allowed.
2: You can not add space in variable name, but you can use underscore.
1 2 3 4 5 6 |
#allowed hello_world = "Hello World" #not allowed hello world = "Hello World" |
3: Don’t use Python built in keywords and functions as your variable name.
1 2 3 |
#not allowed print = "Hello World" print(print) |
4: Variable names are case sensitive. name and Name are different variables.
1 2 3 4 5 |
name = "Parwiz" print(name) Name = "codeloop.org" print(Name) |
Run this code and this will be the result.
1 2 3 4 |
Parwiz codeloop.org Process finished with exit code 0 |
Subscribe and Get Free Video Courses & Articles in your Email