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.
#!/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 = 10 a = "GeeksforGeeks" print(a)
How does + operator work with variables?
#!/usr / bin / python a = 10 b = 20 print(a+b) a = "Geeksfor" b = "Geeks" print(a+b)
Can we use + for different types also?
No using for different types would produce error.
#!/usr / bin / python a = 10 b = "Geeks" print(a+b)
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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Monkey Patching in Python (Dynamic Behavior)
- Python | Pandas DataFrame.nsmallest()
- Python | Pandas dataframe.drop_duplicates()
- Python | Pandas DataFrame.nlargest()
- Python | Difference between Pandas.copy() and copying through variables
- Python | Pandas Dataframe.sample()
- Python | Arithmetic operations in excel file using openpyxl
- Python | Random Password Generator using Tkinter
- Python | Pandas.to_datetime()
- Python | Convert Image to Pdf using img2pdf module