Introduction to python - Part 3

Photo by Andrew Neel on Unsplash

Introduction to python - Part 3

ยท

6 min read

Welcome to my new blog. In our previous blog, we wrote our first python code by learning how to use python's print statement, comments, and escape characters. So in this blog, we will understand the variables in python.

Variables in python

In Python, variables are used to store data values. A variable is a name that refers to a value stored in memory. Variables are nothing but the name of the containers which store the data. In this blog, we will discuss variables in Python and their use cases.

Creating Variables

Python has no command for declaring a variable. A variable is created the moment you assign a value to it. It is very easy to create variables in python compared to other languages.

x = 10
y = "Tom"
print(x)   # It just prints 10 into the console.
print(y)   # It prints the word Tom into the console.

There is no step like declaring a variable in python. Python declares the variable itself when it is created.

a = 4       # a is of type integer.
b = "Sally" # b is now of type string.

We will be learning the details of strings, integers, and all other data types in the coming blogs. Now just know that integers are integer numbers and a string is nothing but a set of characters that are included in quotes.

Rules For Naming Variables

A variable can have a short name (like x and y) or a more descriptive name (age, name, total_area). Here are some rules for naming the variable names.

  • 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, a-z, 0-9, and _ ).

  • Variable names are case-sensitive (age, Age, and AGE are three different variables)

  • A variable name cannot be any of the Python keywords (We will see what are python keywords in upcoming blogs.)

Multi-Word 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 and understandable.

Camel Case

Camel Case is a naming convention that uses capital letters to indicate the start of each word within a variable name. The first word in the variable name is lowercase, and the first letter of each subsequent word is capitalized. For example:

theVariableName = 'tom' 
#you can see that except for the first word the other words are capitalized.

Pascal Case

Pascal Case is a naming convention that uses capital letters to indicate the start of each word in a variable name. The first letter of every word is capitalized, including the first word in the variable name. For example:

TheVariableName = 'tom'
#you can see that every word starts with a capital letter.

Snake Case

Snake case is a naming convention that separates words with underscores. In snake case, all letters are lowercase, and underscores are used to separate words. For example:

the_variable_name = 'tom'
# every word is separated by an underscore.

Assigning Multiple Variables

In Python, you can assign multiple variables at the same time using a single line of code. This feature is called multiple assignments or tuple unpacking. It can be a very useful technique to make your code more concise and readable.

Let's take a look at some examples of how to use multiple assignments in Python.

x, y, z = 1, 2, 3
print(x)
print(y)
print(z)

Output will be:

1
2
3

In this example, we are assigning the values 1, 2, and 3 to variables x, y, and z respectively. Each variable is separated by a comma, and the values are also separated by commas. When we print the variables, we can see that they have the expected values.

a, b = 10, 20
a, b = b, a
print(a)
print(b)

The output will be,

20
10

In this example, we are swapping the values of variables a and b using multiple assignment. We first assign the values 10 and 20 to variables a and b respectively. Then, we assign the value of b to a, and the value of a to b. This results in a and b swapping values, which we can see when we print the variables.

Global and Local Variables

In Python, variables can have different scopes depending on where they are defined. The two most common types of variable scopes are global and local. Understanding the differences between these two types of variables is important for writing clean code.

Global Variables

Global variables are defined outside of any function and can be accessed from anywhere in the code. They have a global scope, which means that they are available throughout the entire program. Global variables can be accessed and modified by any function in the program.

Here's an example of a global variable:

# Define a global variable
my_var = 10

# Define a function that accesses the global variable
def my_function():
    print("The value of my_var is:", my_var)

# Call the function
my_function()   # Output: The value of my_var is: 10

In this example, we define a global variable my_var outside of any function. We then define a function my_function that accesses the value of my_var. When we call my_function, we can see that it correctly prints the value of my_var.

Local Variables

Local variables are defined inside of a function and have a local scope. They can only be accessed from within the function that they are defined in. Once the function exits, the local variables are destroyed and their values are lost.

Here's an example of a local variable:

# Define a function that uses a local variable
def my_function():
    my_var = 10
    print("The value of my_var is:", my_var)

# Call the function
my_function()   # Output: The value of my_var is: 10

You can understand the difference between global and local variables with this example.

# Define a global variable
x = 10

# Define a function that tries to modify the global variable without using "global"
def my_function():
    x = 20   # This creates a new local variable "x" with a value of 20
    print("Inside the function, x =", x)

# Call the function
my_function()   # Output: Inside the function, x = 20
print("Outside the function, x =", x)   # Output: Outside the function, x = 10

Although this was a long blog I think this covers most of the basic concept on variabes. In the next blog we will be starting with data types in python.

I hope that you guys have understood what I tried to explain. Now that's it for this blog, we will be learning some more things in the upcoming blogs. Till then keep learning.

Please do like, share, and follow for more content like this ๐Ÿ˜€.

You can connect with me on Linkedin |GitHub
if you have any suggestions please comment below, Thank you :)

ย