To watch the video demonstration/tutorial, please watch:
In this tutorial, we will learn about storing pieces of data into variables. So what is a variable? A variable is how you hold on a piece of data so that you can use that data in whatever way you want. Let's try to explain it in a very non technical way.
Imagine you have access to an object, say an apple. You are allowed to see it and touch it, but you have no way of holding on to it. So you come across it, you may use it once but since you cannot hold on to it, you lose it after first use. What if you want to hold on to this apple so that you can use it again and again, or save it for use later, or mix it with other fruits? So you get a basket and you put your apple in that. Now you have that basket that is holding the apple and you can access it any time you want. This basket is your variable, that is holding the data, which is the apple!
It is very simple to create/initiate a variable in python. A variable can be represented by any letter or number or a combination of both. So for example we want a variable named "basket" and we want it to hold the text "apple". This is how we would create it in python:
basket = "apple"
Note that basket is not in quotation but "apple" is. Basket is the name of the variable and now every time we write basket in code, it would be referring to this variable, regardless of what it is holding. It would be a simple letter:
a= "I am a variable"
In jupyter notebook, it is very easy to check a value of a variable, you can just type in the name of the variable and it will display the assigned value or the information it is holding.
Operations with variables
Variables are how you hold data and how you manipulate it and do different things with it. Therefore they are once of the core concepts of any programming language. Python is pretty intuitive and manipulating variables are easy.
Assigning a new value
Simply write the name of the variable and give it a new value. For example:
a = 1
To reassign a new value, I can write:
a = 2
Now when I check, the value of a is changed to 2.
Mathematical operations:
Python makes is very simple, the syntax is very intuitive. Lets say, we have two variables:
a=2
b=5
To add them, we can simply write:
a+b
the output would be 7, the sum of the two. Same is true for other mathematical operations. You can also store the result of the operation in a new variable. So:
sum = a+b
Checking the value of sum:
sum
7
Now what happens if your variable does not have numbers and has text and you try to add them? The answer is simple, what happens when you combine the contents of two baskets into one? You get a combination of the two variables. So:
a="one"
b="two"
a+b
onetwo
Use Scenario:
Let's have a simple project. We want code that takes input from the user, and then displays a greeting with the user provided name.
To get user input, we can use the input method:
input('What is your name? ')
This line will get user input however, you are not storing it any where and thus cannot use it. Therefore, we will save it in a variable "name".
name = input("What is your name? ")
Whatever input the user provides, will be stored in the variable name. Now we can refer to that input by the variable name.
print("Hello "+name+"!")
Comentarios