Analyzing the code, line by line
Its time to work on our first program together! We will write a program to input the First name, Last name, age and sex, to combine all of them into one sentence. Try thinking about it before proceeding further. How would you do that? First name, Last name and sex would be a string
, while age would be an integer
. We could concatenate all of them together to form a sentence. Wait, how can we concatenate an integer
with a string
? This is easy, we change the Data Type of the integer
to a string
after the input. Now, how do we take an input?
TRY IT YOURSELF: Try finishing the task above before proceeding forward. Use Google for help if you need, but give it a shot.
Lets type out the program:
Note: We type this program on the textpad to the left side on the Repl platform.
# This program inputs various parameters and outputs them as a sentence
print('Hello, hope you are doing well today. What is your First name?')
firstName = input()
print('Good to know you, ' + firstName + '. What is your Last name?')
lastName = input()
print('Hello, ' + firstName + ' ' + lastName + '. May I ask your age?')
age = input()
print('Lastly, may I know your gender?')
gender = input()
print(firstName + ' ' + lastName + ' is a ' + str(age) + ' year old ' + gender + '.' )
This might seem like a lot of code, so lets study it line by line:
# This program inputs various parameters and outputs them as a sentence`
This is a comment in a Python program. Everything after that #
(in the same line) is not included during a program run. Python ignores comments, and you can use them to write notes or remind yourself what the code is trying to do, just like we did at the start of the program.
PRO TIP: Another use case could be to put a
#
before a piece of code you do not want to run. This is called commenting out the code and can be used to figure out the error when the code doesn't run well.Interesting Fact: Python also ignores the blank line after the comment. You can add as many blank lines to your program as you want. This can make your code easier to read, like paragraphs in a book.
print('Hello, hope you are doing well today. What is your First name?')
The print('')
is used to print the text in the String ' '
onto the repl console. Remember how we did a print out for Hello, World
in the first unit of this chapter?
When Python executes this line, we say that Python is calling the print() function and the string value is being passed to the function. A value that is passed to a function call is called an argument. Observe that the quotes are not printed to the screen. They just mark where the string begins and ends; without being a part of the string value.
print('Hello, ' + firstName + ' ' + lastName + '. May I ask your age?')
Here, we print the name by calling the variables in the print()
function. As you can see, we added extra space strings for this part: firstName + ' ' + lastName
to make sure there the First name and Last name is separate.
Note: You can also use this function to put a blank line on the screen; just call
print()
with nothing in between the parentheses.
Moving on to the input()
function. This function waits for the user to type something on the keyboard and press the ENTER key.
firstName = input()
Here, the user inputs their First name, which is assigned to the variable firstName
.
Lets analyze the last part of this code, which might seem new to you.
print(firstName + ' ' + lastName + ' is a ' + str(age) + ' year old ' + gender + '.' )
The part where we use str(age)
is quite easy to understand. As we mentioned before, age is an integer
since its a number. We use str()
to simply convert the data type into a String. Because we cannot concatenate two variables of different data types, otherwise it gives an error.
>>> 'I am ' + 10 + ' years old.'
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
'I am ' + 10 + ' years old.'
TypeError: Can't convert 'int' object to str implicitly
This is why we have to explicitly convert an Integer into a String or vice versa.
PRO TIP: We can use
int()
andfloat()
for type conversion to convert to Integer and Floating point respectively.
As we have studied our program thoroughly, lets run it now!
>>>
Hello, hope you are doing well today. What is your First name?
Alan
Good to know you, Aman. What is your Last name?
Walker
Hello, Aman Middha. May I ask your age?
21
Lastly, may I know your gender?
Male
Alan Walker is a 21 year old Male.
PRO TIP: You can pass the len() function a string value (or a variable containing a string), and the function evaluates to the integer value of the number of characters in that string. Example: For
name = 'Alan'
,len(name)
will output 4.