Python Programming Practice- TYPING THINGS IN
The argument to the print function is the calculation. Python will do the calculation and print out the numerical result.
This program may seem too short and simple to be of much use, but there are many websites that have little utilities that do similar conversions, and their code is not much more complicated than the code here.
A second program Here is a program that computes the average of two numbers that the user enters:
num1 = eval ( input ( ' Enter the first number: ' )) num2 = eval ( input ( ' Enter the second number: ' )) print ( ' The average of the numbers you entered is ' , (num1+num2)/2)
For this program we need to get two numbers from the user. There are ways to do that in one line, but for now we’ll keep things simple. We get the numbers one at a time and give each number its own name. The only other thing to note is the parentheses in the average calculation. This is because of the order of operations. All multiplications and divisions are performed before any additions and subtractions, so we have to use parentheses to get Python to do the addition first.
Typing things in
Case Case matters. To Python, print , Print , and PRINT are all different things. For now, stick with lowercase as most Python statements are in lowercase.
Spaces Spaces matter at the beginning of lines, but not elsewhere. For example, the code below will not work.
temp = eval ( input ( ' Enter a temperature in Celsius: ' )) print ( ' In Fahrenheit, that is ' , 9/5*temp+32)
Python uses indentation of lines for things we’ll learn about soon. On the other hand, spaces in most other places don’t matter. For instance, the following lines have the same effect:
print ( ' Hello world! ' ) print ( ' Hello world! ' ) print ( ' Hello world! ' )
Basically, computers will only do what you tell them, and they often take things very literally. Python itself totally relies on things like the placement of commas and parentheses so it knows what’s what. It is not very good at figuring out what you mean, so you have to be precise. It will be very frustrating at first, trying to get all of the parentheses and commas in the right places, but after a while, it will become more natural. Still, even after you’ve programmed for a long time, you will still miss something. Fortunately, the Python interpreter is pretty good about helping you find your mistakes.
One Response
[…] Python Programming Practice- TYPING THINGS IN […]