Assignment of Variables

Written by Harnoor Chima (8/12/20)

Introduction

In this lesson, we will go over the basics of creating variables and assigning values to them in python. As well as how to reassign variables that already exist to new values.

Creating a Variable

Creating a variable is very simple and you can use almost any combination of characters to name it. You can assign numerical values to variables or you can assign a string of characters to a variable.

x = 5
y = 3

name = "evan"
car = "sedan"

Once a variable is created at any point in your code, it can be used anywhere else within that project.

Reassignment

Python is a dynamic programming language which allows variables and functions to be reassigned to different values even if they already have a value.

Reassigning Variables

It is very easy to reassign a variable to a new value even if it was already given a value previously.

x = 5
print(x)

x = 3
print(x)

In this example, we assign x to 5 and print x to get the value of 5 and then we reassign the variable x to 3 and the print function returns 3.

Reassigning Functions

We can apply a similar concept to functions because we can assign a specific function to a variable.

max(2,4,5)

h = max

h(2,4,5)

This example uses a function called 'max' which gives us the highest value in a list of numbers which in this case is '5.' We reassign the function 'max' to the variable 'h' which allows us to use the function without calling it directly giving us the same output of '5.'

Last updated