Variables - Python Programming Basics #1.3

Programming is all about problem-solving. We store information, make the information go under some kind of process and then expect some kind of result as the outcome.

So, let's learn to program by making a program that will greet the person who provides his/her name.

Open IDLE or your preferred text editor linked with Python.

The flow of the program will be the following:

  1. Start program
  2. The user inputs his/her name Eg. John
  3. The program prints: Hello John! Nice to meet you
  4. End program

We need to make a program the implements the above steps. The steps we have written above is an algorithm. An algorithm is the description of the steps of a program in a layman language.

Let's write the code to accomplish it.

The first real step is to take input from the user. To take input, we need to use the input function provided by Python. A function in programming is a chunk of code that is frequently needed by the program to do a specific task. In the future, we will make our own custom functions.

name = input()

"name" is the place where the value provided by the user will get stored. "name" is a variable. A variable is a container for storing information. Just like a jar in a kitchen can store anything, a variable can store any value.

After that, if you write

print(name)

The value in the name variable will be printed back.

But, we had planned to print it in the manner - Hello "name"!. To do that

print("Hello " + name + "!")

When python will read this statement, it will place the value of the name variable in the print statement and then execute it. So the result will come out to be

Hello John!
 

Now, variables are not just limited to being text. Variables can also contain numbers. These are different "data types". But in python, you don't need to specify the type of the variable. Also, you can change the type of a variable any type. You can check the type of any variable by giving that variable to another python in-built function called type.

Let's see an example.

 

Excercise

Make a program to take the birth country and birth city and display the obtained values in the below format:

Your birth country is ..............

Your birth city is ..............