BASIC FUNDAMENTALS BEHIND PYTHON
Devansh Hirani
Founder, Director, and Operations Manager at Hirani Analytics Pvt. Ltd | Transforming Industries with Data-Driven Insights | Converting Data into Real-Time solutions with Artificial Intelligence and Machine Learning
Okay! The theory of programming cognition is the intent which will trigger a spark in your career as a Python Developer.
Need of the hour is to focus upon computational thinking whereas on the other side developers need to be conscious about their grasping abilities to vanquish the challenges of code readability which is the cornerstone of an intensive dynamic environment.
This blog will deal with the basic nomenclatures which are being used in python nowadays and alongside this implementing a problem statement as we progress through the learning path would make things even more easier.
Let’s kick-start our journey towards Python.
- So, what does the problem statement say?
A python program needs to be created in such a manner that after a user enters his name and current age, the output should be the year wherein the user’s age would be 45.
- Say, for example if a user’s current age is 44; then according to the problem statement the output of the program would be 2020 since the current year being 2019 and the user’s age 44, so obviously the user would turn 45 the next year which is 2020.
Now the question that would arise will be in accordance with the different kinds of approaches which must be followed in order to get the appropriate output.
Ways to go about:
1. Getting an input from the user. How is it possible in python?
Here we go:
The command to get a user input in python is input(). But, even if a user enters a number; the resulting output will be in the form of a string.
2. The function int() turns a string into a number. Then the rest of the processes can be easily carried out.
STEPS TO START SOLVING THE PROBLEM STATEMENT
- First of all open python instance, i.e. Jupyter which is nothing but a platform where python coding can easily be carried out.
- Create a variable called Name.
Name = input(‘What is your name?’)
This statement is a user defined statement which means the question regarding the name would be asked to the user.
- Then we want to ask a user his age. The command would be:
age = int(input(‘How old are you?’))
Since age is a number, that’s why the command ‘int’ is being used. And ‘input’ command is used for asking a question to the user regarding his age.
Hence int and input have been merged simultaneously.
- After this a variable named WhatYear will be created to know the current year which will be an input given by the user.
WhatYear = int(input(‘Please specify the current year’))
- Then a variable year is created with type string and its value is
(WhatYear – age)+45
Because the difference between the current year and current age added with 45 will yield the year in which the user’s age would turn 45.
- Finally the print statement will be used to state the final statement which is
Name will turn 45 in the year…
print(Name + ‘will turn 45 in the year’ + year)
Example: XYZ will turn 45 in the year 2098
FINAL PROGRAM WOULD BE SOMETHING OF THIS SORT:
Line 1: Name = input(‘What is your name?’)
Line 2: age = int(input(‘How old are you?’))
Line 3: WhatYear = int(input(‘Please specify the current year’))
Line 4: year = str ((WhatYear – age)+45)
Line 5: print(Name + ‘will turn 45 in the year’ + year)
FINAL OUTPUT:
What is your name? Dev
How old are you? 24
Please specify the current year 2019
Dev will turn 45 in the year 2040
Let us try and understand the output of the program:
Dev’s age is 24 and the current year is 2019.
Subtract current year and Dev’s current age. That is 2019 – 24 = 1995
Add 45 to 1995 = 2040 which is the expected output.
CONCLUSION:
In this blog we came across two main functions used in python.
1. input()
2. int()
And using these functions we solved a 5-line program, understood the importance of the above stated functions alongside with the steps to solve the problem statement. The program and as well as its output were explained briefly and in a user-friendly manner.
Happy Reading!
Thank you!