Table of Contents
Understanding variables and Assigning values
What are variables?
On a bookshelf, we can see sections that are precisely labeled. So, if we want to place a mathematics book, we will keep it in the section labeled Mathematics, right? so a bookshelf can be used to store the books we want .
Similarly, in programming, variables act as containers to store values. These values can be anything, from numbers to text or entire functions. And just like the labels in the case of the bookshelf, variables are identified by variable names. The values stored in the variables can be used further in our programs.
We can retrieve them by referring to the variable names which are used to identify these variables.
Storing values in variables
The variable name, value, and assignment operator (=) are the three things required to store values in a variable.
myVar = 70
Here, myVar is the variable name and it is assigned a value of 70 using the assignment operator(‘=’).
So, myVar is a variable with the value 70. If you know other languages, like C++, and Java, you will note that variables don’t have to be declared.
Types of Variables in Python
Strings Data Type
When the value of a variable contains text i.e one or more words, it is called a String in programming.
Think about your name. It contains one or more words. Thus, your name is a String. Your pet’s name, your building’s name, etc. all are Strings.
A string is a sequence of characters. Anything written between “ ”(double quotes) or ‘ ’(single quotes) is a string in Python.
myVar1 = "Hello"
Here, myVar1 is a string with the value “Hello”.
“Hello3”, “456”, ‘Hello’, ’45Number’ are examples of strings in Python.
As “456” is written inside “ ” (double quotes), it is considered a string in Python.
Numeric Data Type
If you ask other people, what is their birth date, the answer will be a number. Some people may have 5 or 23 or 10 and so on. When the value of a variable is a number, it’s called a numeric type variable.
There are a few types of numbers. If your number doesn’t have a fraction i.e a decimal value, then it is called an Integer number. Thus, 23,9,17,22, etc are Integers. When a number has a decimal like 2.5, 56.8, etc. They are called Float numbers.
Let’s see how numbers are stored in Python.
myVar = 2 # An integer
Here, myVar which has a value equal to 2 which is an integer.
myVar2 = 21.54 # A floating point number
Here, myVar2 has a value of 21.54, and thus it is a floating point number. Regardless of the type of number, whether a floating point or an integer, Python views them as a single category – Numbers. Python automatically identifies the type based on values and we do not need to specify the type manually. That’s how different types of variables can be created in Python.
Booleans: What are they?
Taking user input in Python
While creating your account on any social media platform or any other platform such as Netflix, we need to provide our name, email, etc.
This means the application needs to take user input.
To ask for any information or input from the user in Python, we just need to use the input() function. This will ask the user for some input.
While asking for user input(), we can add extra information to tell the user what information they should provide, For example;
input(“Enter your name:”)
Thus, the above code will take the name of the user as input. The data type of the input will be a string and we remember we discussed this while comparing Python2 and Python3.
You must be thinking, what should we do with the user input, or where should it be stored?
You can store user input in a variable and then use that variable for further processing this input information.
name=input(“Enter your name:”) print(name)
We understand that Python treats user input as a string value,
Thus while processing information for numeric type, we need to tell the interpreter about it.
age=int(input(“Enter your age:”))
Or something like,
temperature= float(input(“Enter the temperature ”))
In Programming, variables are containers that can be used to store values. Values can be numbers or strings. Strings are written inside ” “(double quotes) or ‘ ‘ (single quotes).
These variables are referenced using their variable names.
Leave A Comment