Storing data in a python program

To accomplish or perform any calculation, we need some data in our program. That data may come from user input or we can store our own data statically by assigning a unique value like how we assign a value in MS excel cell.
In python, those data are grouped into different types. Those types are given a unique name in english word and in shortly, we call the concept of “types of data” as “data types” in programming terminology. To store the data using a particular data type, we use the concept called as variables.

Variables

A container that is used to store some data is called as variable.
We will store the data by forming a variable.

Forming variable in python

We will form a variable to store any data as mentioned in below line:
variable name=value
In python program, every variable has a unique name that we type it first, and for assigning, we use “=” (equals symbol) and after that, we type the exact value what we want to store inside the variable.
Any value in a program that are stored in variable is called as literal So, the formal syntax for constructing a variable with some basic data type is as follows:
variable_name=literal.

Rules and restrictions for naming a variable

Also, in python program, there are some rules and rrestriction to name the variable.
Some of those rules and restrictions are given below:

  1. Variable name should always start from either alphabet (a to z) or “_” (Underscore symbol).
  2. Variable name should not start from any numbers (0 to 9) but, can contain in the middle or at the end of the variable name.
  3. Variable name should not contain any symbols except “_” (Underscore symbol).
  4. Variable names are case sensitive (When you assign a variable name called “EMAIL” which all letters are in uppercase, you will always refer the value stored in the variable using its genuin name called “EMAIL” which all letters are in uppercase, but not “email” which all the letters are in lowercase).
  5. Variable name should not be any keywords those are used in python program.

Types of data (Data types) in python

Below We will go through the various basic data types available for us in python and storing technique of the data.

Integer data type

Its one of the type of data (Data type) in python where its used to store any numbers as its value.
Example of integer data:
0, 1, 2, 3, -1, -2, -3 etc.
The limitation of this data type is, It is unable to store the numbers that contains the digits after the decimal point like 10.5, 12.2, 15.5 etc. Where in this case, it may only store like 10, 12, 15.
Example of storing int data in a variable:
days=365

Float data type

Float is one of the basic data type in python which is similar to integer data type. Float will overcome the limitations of integer. It means, You can store numeric value of with or without decimal.
Example of float data:
10.25, 20.50, 30.10 etc.
Example of storing float data in a variable:
pi=3.14
You may be asking a question yourself, Why can’t I store any words or any sentences in a variable?
Of course you can store in variable, but, the important point to keep in mind is int and float are only used when you want to store only numeric type of data.

Bool data type

Bool is a data type in python, capable of only storing True or False value. We will use this type of data in the situation of having
one of the 2 outcomes in performed actions. In plain English, we can say that the out come will be correct or wrong.
Here, In this data type, there are 2 keywords of python involved.
Those keywords are “True” (Which represents logic or comparison is correct) and “False” (Which represents logic or comparison is wrong).
Also, bool is a short form for the word boolean.
Examples of such action includes:

  • A light bulb, that will result in off or onn.
  • A box, that will result in open or close.
  • A quiz game, where answer of a question results either results in True or False.

You will be getting this type of data when you perform some logics or comparisons on any value. We will deep dive into it in future tutorials.
example of storing bool data in variable:
answer=True
light=False

String data type

Lastly, to store any textual data, (whether it may be number, symbols or alphabet), We use string data type in python.
Example of storing string data in variable:
website=”pwvi.org”
Storing of any single line text in a variable should happen between the opening and closing quotation mark (“<Some text here>”).
Storing of any multiple line text should happen between the triple quotation mark
(“””
<Some multiline text here>
“””)

Feature of python to be noticed while storing any variable

If you assign any value to a variable using “=” (equals symbol), the type of the data that you are providing is automatically understood by python virtual machine that helps to run python programs.
To test it, assign any value to a variable using proper syntax, in the next line, include the function name called type with the proper variable name to retrieve its type.
If you only include type function, The output of the variable type is not displayed. For displaying, Embed type function inside the print function.
Example python program to demonstrate type function:
language=”Python”
version=3.9
print(type(language))
print(type(version))
Observe the type of language gives the output of <class str>, and the type of version gives the output of <class float>.
Here, “str” means, String data type.

Disadvantage of python to be noticed while storing any variable

Hence python understands what type of data you are providing to a variable, It may lead to slower program execution since it may want to do the analysis of provided data to retrieve its type.

Example of a python program to demonstrate all basic data types

# this type of line is a comment which starts from hash symbol, and is not at all executed by the program and you can delete this line after reading
# python program to calculate the runrate of a cricket match’s score.
# below line, we are using input function, that function takes input from the user’s keyboard and storing it in variable.
# By default, input function gives the user entered value as a string data type.
team=input(“Enter the name of the team”)
# In the below line, we are including int function and embeded input function inside that.
# because, input function gives user entered value as string, that string value should be converted into integer type by us. Without converting, we cannot perform arithmetic operation on string type of data.
score=int(input(“Enter the present score”))
overs=int(input(“Enter the present overs bolled without including decimal points”))
# Below we are converting string into boolean
international=bool(input(“Is it an international match? Enter True or False”))
runrate=score/overs
# to get the runrate, we are dividing the score with runrate. The “/” (slash symbol) is used to divide first value with second value.
print(“Present runrate:”)
print(runrate)
print(“Type of runrate variable:”)
print(type(runrate))

output of the program

Enter the name of the teamIndia
Enter the present score250
Enter the present overs bolled without including decimal points40
Is it an international match? Enter True or FalseTrue
Present runrate:
6.25
Type of runrate variable:
<class ‘float’>

Published by Harsha HD

Myself Harsha, I'm a person from Bangalore, Karnataka with 100% visually challenged. I completed my PG in masters of computer application at Bangalore university in the year 2022. With my coding interest along with interest of sharing those knowledge to our visually challenged community, I founded this website, trying to provide accessible coding materials and training specially for visually challenged people with affordable cost. So, that the VI people will able to perceive their education in technical/electronics stream in an accessible way.

One thought on “Storing data in a python program

Leave a Reply

Your email address will not be published. Required fields are marked *