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)

Output:

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)

Output:

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)

Output:

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)

Output:

GeeksforGeeks

How does + operator work with variables?

#!/usr / bin / python
  
a = 10
b = 20   
print(a+b)
  
a = "Geeksfor"
b = "Geeks"
print(a+b)

Output:

30
GeeksforGeeks

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)

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"    

Output:

cse
cse
101
cse


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.