In this tutorial we are going to learn :

  • Variables in Python
  • Variable Name’s Rules
  • Creating Variables
  • Re – Declare Variables
  • Concatenate Variables

 

 

Variables in Python

 
  • Variable is a named location for storing data values.
  • Unlike many other programming languages, variables don’t need to be declared any particular type.
  • Every value in Python has a different data type. Different primitive data types in Python are integer, string, float, boolean, etc.

 

 

Variable Names Rules

 
  • A variable name must start with a letter(either upper or lower case ) or an underscore _ character.
  • They can contain numbers, letters or underscore characters but (cannot begin
    with a number.
  • Variable names are case-sensitive (student_number, Student_number, STUDENT_NUMBER are three different variables).
  • Variables are created when they are first attached to a value, using =.

 

 

Creating Variables

 
  • Different most used primitive data types in Python are integer, string, float, and boolean.
 

Example : 

# creating variables 

student_numbers = 200        # integer 
product_name = "Computer"    # string
number = 12.9                # float
is_right = True              # boolean

#printing variables 
print(student_numbers)
print(product_name)
print(number)
print(is_right) 

Re – Declare Variables

 

  • We can also change variables after we create and declare them

 

Example : 

# re - declare variables 

student = 200
print(student)

student = "James"    # re - declare variable
print(student) 

 

 

Concatenate Variables

 
  • In Python, we cannot concatenate different types of variables.
 

Example : 

  • Correct Uses of Concatenate Variables.

We used the str() function to convert int to string. so we can concatenate them.

# Concatenate Variables 

first_number = 10
second_number = 20
age = 21
name = "James"
comment = "Awesome"

# Correct uses - Concatenate Variables
print(first_number + second_number)                # int + int 
print("Python is " + comment)                      # string + string
print(name + " is " + str(age) + " years old. ")   # String + int 
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments