The Beginner’s Guide to Python Variables

Variables in any programming language are like a small temporary container. which is used to store the data for temporary purposes in a ram.

Let’s see how to declare a variable in python and what other things we can do with python variables.

Creating Variables

Python has no rule or command for declaring or defining a variable.

A variable creates a space inside RAM when the first value is get initialized to it.

Example

x = 400 # x is of type intx = “Wakeupcoders” # x is now of type strprint(x)

Python Rules for Naming a Variable:

In a python, we can declare a variable like a, b, or c or it's always better to give some meaningful and descriptive name like car, book, or age as per the requirement.

Let’s see some of the rules of declaring variables.

* Variable should always start with an alphabet letter or we can also use underscore.

* We can not initialize with the number.

* Variable names are always case sensitive.

* Variable should contain alphanumeric characters, numbers and underscores.

How to Handle Multi-Words Variable Names

In a multi-word variable, it could be difficult to read. Let’s see how we can make it more convenient to read.

Camel Case

myVariableName = “Wakeupcoders”

Pascal Case

MyVariableName = “Wakeupcoders”

Snake Case

my_variable_name = “Wakeupcoders”

How to Handle Assigning Multiple Values to Variables

We can assign multiple values to variables.

Assign Many Values to Multiple Variables

x, y, z = “Wakeupcoders 1”, “Wakeupcoders 2”, “Wakeupcoders 3”print(x)print(y)print(z)

Assigning One Value to Multiple Variables

x = y = z = “Wakeupcoders”print(x)print(y)print(z)

Unpacking a Collection

fruits = [“apple”, “banana”, “cherry”]x, y, z = fruitsprint(x)print(y)print(z)

How to Use Global Variables in Python?

Global Variables

Global variables are always declared outside of a function and they can be accessed both, inside and outside of the function.

Example:

x = “Wakeupcoders”def myfunction():print(“Python by “ + x)myfunction()

When you create a variable with the same name inside a function, it becomes local and can only be used within the function. The global variable with the same name will remain global and have the same value as before.

Example:

x = “awesome”def myfunc():x = “Wakeupcoders”print(“Python by “ + x)myfunc()print(“Python is “ + x)

Global Keywords in Python

When you create a variable within a function, it is usually local and can only be used within that function.

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

Example:

def myfunction():global xx = “Wakeupcoders”myfunc()print(“Python by “ + x)

End of Blog

I hope this information regarding Python will be useful to you guys, be connected with us for more info like this on python.

Do you love how we teach and look for learning with us?

Let’s have a quick connect form below details

Call or WhatsApp: +91 888–387–8884

Email: Info@wakeupcoders.com

I hope this blog was helpful for you guys, if you like reading our blog then do like, share and comment, keep connected with us for new blogs, and then keep reading and learning. thank you!!

Credit:

Narayan Jha(author), Purnima Sinha(editor), Anjali Jha(digital marketing)

--

--

Wakeupcoders - Digital Marketing & Web App Company

We make your business smarter and broader through the power of the internet. Researcher | Web developer | Internet of things | AI | www.wakeupcoders.com