Table of Contents[Hide][Show]
Welcome to the third lecture in this series.
By now you should be able to install Python on your computer and write a simple program by assigning variables and printing a simple statement on the terminal.
Now, things get a little more interesting and we will see how to use inputs and much more.
Receiving Input
Now we will learn how to receive input from the user in Python. We’re going to write a small program that asks for the user’s name and then we’ll print a greeting message customized for that user.
So instead of print, we’re going to use input. Now both these input and print are functions that are built into Python.
To understand this better, think of the remote control of the TV.
On this remote control, we have a bunch of buttons, these are the functions built into your tv, you can turn it on, turn it off, change the volume and so on. In Python, we also have functions for common tasks such as printing messages, receiving input, and so on.
We’re going to use the input function. Now type;
input('What is your name? ')
This input function will print this message on the terminal, and then it will wait for the user to enter a value. Whatever the user enters, this input function will return. Now we can get that value and store it in the memory using a variable. We can save it in a variable ‘Name’. Like this:
name = input('What is your name? ')
Now on the second line, type:
print('Hi' + name)
In this way, we have combined one string “Hi” with another string “name” in the same expression. The output will look like this.
Excercise
Now here’s an exercise for you. Pause scrolling and try this.
Ask two questions: Person’s name and favourite colour. Then print a message like “Shahbaz likes Black”
Here is the solution. “DON’T CHEAT”
Type:
name = input('What is your name? ')
favourite_colour = input('What is your favourite Colour? ')
print(name + ' likes ' + favourite_colour)
The output will look something like this:
Type Conversion
Let’s understand this through an example.
Say you want to determine your age through a simple Python program. Which would be:
birth_year = input('Birthyear: ')
age = 2022 - birth_year
print(age)
Now apparently this program should work but if you run this program, you will get an error.
That’s because like other programming languages, Python too has distinct data types and your input of birth year is taken as a string by the Python interpreter despite it being an integer value. Anything with the function “input” is taken as a string.
In this way, this program will be subtracting a string from an integer. We need to correct this by converting the string in the input expression to an integer.
Here’s how we will do it.
birth_year = input('Birthyear: ')
age = 2022 - int(birth_year)
print(age)
The output will look something like this.
So we have declared the variable “birth_year” as an integer. Other data types in Python include “float” for floating points, “bool” to convert a string to a boolean value and “str” for strings.
Excercise
Now here is an interesting exercise for you.
You know the rules.
Ask a user their weight in pounds, convert it to kilograms and print it on the terminal.
Here is how you will do it.
weight_in_pounds = input('Your weight in pounds: ')
weight_in_kilogram = int(weight_in_pounds) / 2.205
print(weight_in_kilogram)
Wrap Up
That’s all from this lecture.
I will see you in the next lecture, where we will learn about strings.
Leave a Reply