Variable in Python

Variables are names that can be assigned a value and used to reference that value throughout your code. It’s a fundamental aspect of programming. Python lets you create variables to organize and access this information.

Creating Variable:

A variable provides a way to label and access information. It’s kind of like calling your friend on his cell phone. Before you use a variable, you have to create it, as in the following line:

name = “Ali”

This line is called an assignment statement. It declares a variable called name and assigns it the value “Ali.”

An assignment statement stores the value on the right side of the equal sign (=) in your computer’s memory, while the variable on the left side only refers to the value.

Variable Name Rules:

  • Variable names can only contain uppercase and lowercase letters (A–Z, a–z), digits (0–9), and underscores (_). E.g: string1, _a1p4a, list_of_names 
  • Variable names cannot begin with a number. E.g: 9lives 

Choose Descriptive Name:

The name is totally ambiguous. Using a full word makes it a lot easier to understand.  

s = 3600

seconds = 3600

seconds is a better name because it provides more context.

Be consistent:

Variable names in snake case are commonly written as num_students and list_of_names. Every letter is in lowercase, and each word is separated by an underscore.

Follow the traditions of the language:

Some naming conventions are just traditions. For example, variable names start with a lowercase letter. Another tradition is to avoid using an underscore as the first character of your variable names. E.g: per_hour, _second

Keep the length in check:

Long variable names can lead to problems. They can make statements difficult to read. Furthermore, the longer the variable name, the more likely it is a typo.

Don’t use built-in functions:

Variable name can’t be a built-in function name. Like: print, all, any, bool

 

Visited 89 times, 1 visit(s) today