Variables in any programming language are like storage tanks or containers for storing data and the same is in Python as Python Variables store the data and the user performs an operation using/upon those variables.
You can declare a variable by just typing its name and there is no need to assign the type of variable to that variable as python automatically does that for you.
x = 20 y = "I am a variable" z = 'I am also a variable' print(x) print(y) print(z)
20 I am a variable I am also a variable
Also, you can assign a different type of variables to the same variable, see the example below–
x = 20 # integer type x = "aVariable" # string type print(x) # no errors
Rules for Naming a Variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age, and AGE are three different variables)
# legal variable names myVar = 1 YourVar = "var" _thisVar = 20.2 _Thisvar = "notBad" My_Var = "good" MyVar = "Best" # illegal variable names -variable = 12 1variable = "badVarName" my variable = "error"
Multiple Variable Assignment In One Line
Python is the best language if one wants to start learning in the programming world and this is because of its easy syntax here is an example.
You can assign values to any number of variables in a single line, see the example below–
x, y, z = 1, 2, 3 print(x) print(y) print(z)
1 2 3
Assigning one value to many variables in a single line
We can assign a single value to any number of variables in just one line, see the example–
x = y = z = "Hello Dear" print(x) print(y) print(z)
Hello Dear Hello Dear Hello Dear
This is just a two-line program but to convert it to GUI YouTube Video Downloader is a bit longer still, this program of GUI YouTube Video Downloader is less than 25 lines. But still, if you want that two-line program then click here. We have explained this program in every line to make it understand…
Problem Statement: We are given a sentence, we need to word mix in Python means we need to mix the words of the sentence such that new words get formed. How? We need to select a letter from each word from the same index. If there is no letter in the word, do nothing and…
Problem Statement: In this problem of word count in python, we are given a sentence and we need to find the count of all words from the sentence and print them in alphabetical order. For example, sentence=> this is a game of game,result=> a: 1game: 2is: 1of: 1this: 1 Code for Word count in Python:…
Handwritten notes are very important while learning any new programming language. BUY YOUR NOTES NOW WHO AM I? Owner of Python.hub Instagram Page with more than 1.2 million followers.Running this website copyassignment.com which helps in learning programming. Helped more than 3000 people with their projects and doubts. Running a freelancing IT business and helping startups…
Why Learn Python? complete explanation-> Nowadays, we all are wondering through our sights, imagination, and thoughts about the scenario after and along in this pandemic time! But, Yes, ‘Learning doesn’t stop and it Shouldn’t!’, right? So, in this article, we will found the answer to “Why Learn Python?”. There are a lot of…
Data cleaning is the process of ensuring data is correct, consistent, and usable. You can clean data by identifying errors or corruptions, correcting or deleting them, or manually processing data as needed to prevent the same errors from occurring. The quality of data will have a huge impact on the quality of the model. Data…