Python Variables

Python Variables


Variables are data storage containers.

You can also search for these topics, python variables type and exercises, use the declaration rules, program and php variables, what is environment variable in python, what is a python variable, what are the python variable types, what is local variable in python and name.

Creating Variables

Python doesn't have a commanding for declaring variables.

When you first assign a value to variable, it is created.

Example 1 :-

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

Output :-

5
John

Variables with no particular type must be declared, and after they are specified they can even change type.

Example 2 :-

x = 4
x = "Sally"
print(x)

Output :-

Sally

You can also search for these topics, python generate dummy variable, python bound a set of creating multiple variables, python how to assign variables, creating a new variable in python from list, python create variable without value from dictonary, python create variables for each item in list.

Casting

Casting is a technique for specifying the data type of a variable.

Example :-

x = str(3)
y = int(3)
z = float(3)
print(x)
print(y)
print(z)

Output :-

3
3
3.0

You can also search for these topics, python casting list, what is type casting in python, what does casting means in python, casting works in python, python examples, error and exception, python automatic casting, python auto casting.

Get the Type

The type() function can be used to determine the data type of a variable.

Example :-

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

Output :-

<class 'int'>
<class 'str'>

You can also search for these topics, python get the type of a variable, python get the type of exeception, how to get type in python, get the type of a list python, python get type name.

Single or Double Quotes?

Example :- Single or double quotes are used to declare string variables :

x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)

Output :-

John
John


Case-Sensitive

The case-sensitive is important in variable names.

Example :- This will result in the following two variables :

a = 4
A = "Sally"
print(a)
print(A)

Output :-

4
Sally



You can also search for these topics, python case sensitive or not, input, variable. python avoid case sensitive, python is a case sensitive language explain with example, python is a case sensitive python are variables case sensitive, python is a case sensitive comparison, replace the php case-sensitive.

Variable Names and Naming Rules

A variable will have a short name (such as x and y) or a longer name (such as age, carname, or total volume). Python variables have rules :

  • A variable name must begin with a letter or an underscore.
  • A number cannot be the first character in a variable name.
  • Only alpha-numeric characters and underscores (A-z, 0-9, and_ ) are used in variable names.
  • Case matters when naming variables (age, Age and AGE three distinct variables).

Example :- A few variable names are :

# Valid Variable Names
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
# Invalid Variable Names
2myvar = "John"
my-var = "John"
my var = "John"

Note:- Always keep in mind that variable names are case sensitive.

You can also search for these topics, python variable names best practices, which of the folowing are valid python varaible names, in python variable names may start with a digit, invalid python variable names, what are the rules for variable names in python.

Multi Words Variable Names

It can be hard to read names that contain more than one word.

If you want to make them more readable, you can use a variety of methods.

Camel Case

A capital letter appears at the beginning of every word except first one :

myVariableName = "John"

Pascal Case

A capital letter is used to begin every word :

MyVariableName = "John"

Snake Case

The underscore character is used to divide each word in a variable name :

my_variable_name = "John"
You can also search for these topics, three type of multi words variable names, multi word variable names python reserved, python multi word variable names inheritance same, multi word variable names python tutorial, python camel case style example, camel case convention python, python pascal case naming style, python convert to pascal case, python snake case style example.

Python Variables - Assign Multiple Values

You can declare multiple variable and define their value at once in many ways.

Many Values to Multiple Variables

Example :- In Python, you can assign values to multiple variables in a single line by using a function :

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Output :-

Orange
Banana
Cherry

Note :- You'll get an error if the number of variables doesn't equal the number of values.

One Value to Multiple Variables

Example :- It's also possible to assign the same value to many variables in a single line :

x = y = z = "Orange"
print(x)
print(y)
print(z)

Output :-

Orange
Orange
Orange

Unpack a Collection

Python allows you to extract values from a list, tuple, etc. Unpacking is the process of removing the contents of a package.

Example :-

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)

Output :-

apple
banana
cherry



You can also search for these topics, how to assign multiple values to a variable in python, python assign list values to multiple variables, python assign multiple values variable based on condition, python many values multiple variable declare, python one variable multiple values, assign single value to multiple variable python, pack unpack in python, python unpack collection example.

Output Variables

print statements are commonly used to output variables in Python.

Example 1 :- Python + character is used to combine text and a variable :

x = "awesome"
print("Python is " + x)

Output :-

Python is awesome

Example 2 :- Add a variable to another by using the + symbol :

x = "Python is "
y = "awesome"
z = x + y
print(z)

Output :-

Python is awesome

Example 3 :- The + character is a mathematical operator for numbers :

x = 5
y = 10
print(x + y)

Output :-

15

Example 4 :- Python will throw an error if you try to combine a string and a number :

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

Output :-

TypeError: unsupported operand type(s) for +: 'int' and 'str'

You can also search for these topics, python output all variables, python ignore output variable, python output bash variable, input and output variable, basic output with variables python, Example for python Output Variables.

Global Variables

Global variables are variables that are created outside of a function (as in the examples above).

Everybody, both within and outside of functions, can use global variables.

Example 1 :- Create a variable outside of a function and utilise it within the function :

x = "awesome"
def myfunc():
  print("Python is " + x)
myfunc()

Output :-

Python is awesome

Creating a variable with the same name inside a function makes it local, meaning it may only be used within the function. There will be no change to the original global variable with the same name.

Example 2 :- Create a variable with the same name as the global variable within a function :

x = "awesome"
def myfunc():
  x = "fantastic"
  print("Python is " + x)
myfunc()
print("Python is " + x)

Output :-

Python is fantastic
Python is awesome

You can also search for these topics, python global variables not working, what does global variable mean in python, how to avoid using global variable in python, python tutorial global variable, python truly global variable, Example for javascript global variables.

The global Keyword

A variable created within a function is normally local and can only be utilised within that function.

The global keyword can be used to define a global variable within a function.

Example 1 :- The variable is in the global scope if you use the global keyword :

def myfunc():
  global x
  x = "fantastic"
myfunc()
print("Python is " + x)

Output :-

Python is fantastic


If you want to alter a global variable within a function, use the global keyword.

Example 2 :- Use the global keyword to modify the value of a global variable within a function :

x = "awesome"
def myfunc():
  global x
  x = "fantastic"
myfunc()
print("Python is " + x)

Output :-

Python is fantastic

You can also search for these topics, python global keyword usage, how to use global variable in python, python global variable best practices, python need global variable, what is global keyword in python, Example for javascript global Keyword.