Python Variables

What are Variables?

Variables are named location to store data values. You can think of it as a container, which, stores data. Data stored in a variable can be changed. In order to store the data in a program, variable is used.

Variable Declaration

Declaring Python variable is pretty simple. For any variable’s declaration datatype is not required. Just a valid/ allowed variable name with the value is needed. Data type and data value of any variable can be changed at any time.

Example –

For storing age of any employee ->

employeeAge = 30

For storing name of any employee ->

employeeName = “Rakesh”

Variable Naming Convention

Variable names can be the combination on 2 more words. In such scenarios, there are variety of ways using which variables can be named in Python. Those ways are called naming convention. These naming convention can be used to name any variable in Python. Below are the different types of naming convention available for Python variable naming –

  1. Snake Case – Snake case convention uses underscore character ( _ ) as a separator. Using underscore, different words can be joined with each other to form one name. Example – employee_name, employee_age, etc
  2. Pascal Case – Pascal case doesn’t use any separator. Words should be joined with each other starting with capital letter. Example – EmployeeName, EmployeeAge, etc.
  3. Camel Case – Camel case doesn’t use any separator. Words should be joined with each other starting with capital letter EXCEPT first word. Example – employeeName, employeeAge, etc.

Multiple values to multiple variables

Many data values can be assigned to multiple variable simultaneously in just one line.

Example –

empAddress1, empAddress2, empAddress3 = “Kanpur”, “Lucknow”, “Agra”

Same value to many variables

In Python same data values can be assigned to many variables simultaneously.

Example –

firstEmployeeOffice = secondEmployeeOffice = thirdEmployeeOffice = “Bangalore Office”

Video explanation with demonstration can be checkedย here

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *