Variables - zamaniamin/python GitHub Wiki

Variables

Variables are containers for storing data values. variables are like labels for values. we can store a value and give it a name so that we can:

  • refer back to it later
  • use that value to do … stuff
  • change it later on
score = 170

in above code, score is variable and = is assignment and 170 is value

Creating Variables

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

x = 5
y = "John"
print(x)
print(y)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

x2 = 4  # x is of type int
x2 = "Sally"  # x is now of type str
print(x2)

Casting

If you want to specify the data type of a variable, this can be done with casting.

x = str(3)  # x will be '3'
y = int(3)  # y will be 3
z = float(3)  # z will be 3.0

Get the Type

You can get the data type of a variable with the type() function.

x3 = 5
y3 = "John"
print(type(x3))
print(type(y3))

Case Sensitive

Variable names are case-sensitive. This will create two variables:

a = 4
A = "Sally"

A will not overwrite a

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, car_name, total_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (age, Age and AGE are three different variables).
  • Don't use camelCase in variable names.
  • Use underscore in names (pythonic).
  • Don't use python keywords as variable names, see keywords using this command in interactive: help('keywords')
  • use lowercase letters in variable names (PEP8)

Legal variable names

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

Multi Words Variable Names

Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable:

# Camel Case
myVariableName = "John"

# Pascal Case
MyVariableName = "John"

# Snake Case
my_variable_name = "John"

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line: Note: Make sure the number of variables matches the number of values, or else you will get an error.

x4, y4, z4 = "Orange", "Banana", "Cherry"
print(x4)
print(y4)
print(z4)

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

x6 = y6 = z6 = "Orange"
print(x6)
print(y6)
print(z6)

Unpack a Collection

If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables, This is called unpacking.

# Unpack a list:
fruits = ["apple", "banana", "cherry"]
x5, y5, z5 = fruits
print(x5)
print(y5)
print(z5)

Output Variables

In the print() function, you output multiple variables, separated by a comma:

x6 = "Python"
y6 = "is"
z6 = "awesome"
print(x6, y6, z6)