Python Variables
Python is not “statically typed”. We do not need to declare variables before using them, or declare their type. A variable is created the moment we first assign a value to it.
#!/usr / bin / python # An integer assignment age = 45 # A floating point salary = 1456.8 # A string name = "John" print(age) print(salary) print(name) |
45 1456.8 John
Rules for creating variables in Python are same as they are in other high-level languages. They are:
a) A variable name must start with a letter or the underscore character.
b) A variable name cannot start with a number.
c) A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
d) Variable names are case-sensitive (name, Name and NAME are three different variables).
e) The reserved words(keywords) cannot be used naming the variable.
Assigning a single value to multiple variables:
Also Python allows to assign a single value to several variables simultaneously.
For example:
#!/usr / bin / python a = b = c = 10 print(a) print(b) print(c) |
10 10 10
Assigning a different values to multiple variables:
#!/usr / bin / python a, b, c = 1, 20.2, "GeeksforGeeks" print(a) print(b) print(c) |
1 20.2 GeeksforGeeks
Can we use same name for different types?
If we use same name, the variable starts referring to new value and type.
#!/usr / bin / python a = 10a = "GeeksforGeeks" print(a) |
GeeksforGeeks
How does + operator work with variables?
#!/usr / bin / python a = 10b = 20 print(a+b) a = "Geeksfor"b = "Geeks"print(a+b) |
30 GeeksforGeeks
Can we use + for different types also?
No using for different types would produce error.
#!/usr / bin / python a = 10b = "Geeks"print(a+b) |
Output :
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Creating objects (or variables of a class type):
Please refer Class, Object and Members for more details.
# Python program to show that the variables with a value # assigned in class declaration, are class variables and # variables inside methods and constructors are instance # variables. # Class for Computer Science Student class CSStudent: # Class Variable stream = 'cse' # The init method or constructor def __init__(self, roll): # Instance Variable self.roll = roll # Objects of CSStudent class a = CSStudent(101) b = CSStudent(102) print(a.stream) # prints "cse" print(b.stream) # prints "cse" print(a.roll) # prints 101 # Class variables can be accessed using class # name also print(CSStudent.stream) # prints "cse" |
cse cse 101 cse
Recommended Posts:
- Private Variables in Python
- Global and Local Variables in Python
- Class or Static Variables in Python
- Inserting variables to database table using Python
- How to assign values to variables in Python and other languages
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Swap two variables in one line in C/C++, Python, PHP and Java
- Python | Assign multiple variables with list values
- Python | Difference between Pandas.copy() and copying through variables
- Python program to find number of local variables in a function
- CSS | Variables
- Perl | Variables
- How to unset JavaScript variables?
- How to pass PHP Variables by reference ?
- How to pass JavaScript variables to PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



