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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr / bin / python
  
# An integer assignment
age = 45                      
  
# A floating point
salary = 1456.8             
  
# A string  
name = "John"              
  
print(age)
print(salary)
print(name)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr / bin / python
  
a = b = c = 10          
  
print(a)
print(b)
print(c)

chevron_right


Output:

10
10
10

Assigning a different values to multiple variables:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr / bin / python
  
a, b, c = 1, 20.2, "GeeksforGeeks"        
  
print(a)
print(b)
print(c)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr / bin / python
  
a = 10
a = "GeeksforGeeks"     
  
print(a)

chevron_right


Output:

GeeksforGeeks

How does + operator work with variables?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

30
GeeksforGeeks

Can we use + for different types also?
No using for different types would produce error.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"    

chevron_right


Output:

cse
cse
101
cse


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


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