Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

More about input() and type casting

Having a team consisting of the trio input() - int() - float() opens up a lot of new possibilities. 

You'll eventually be able to write complete programs, accepting data in the form of numbers, processing them and displaying the results. 

Of course, these programs will be very primitive and not very usable, as they cannot make decisions, and consequently are not able to react differently to different situations. 

This is not really a problem, though; we'll show you how to overcome it soon. 

Our next example refers to the earlier program to find the length of a hypotenuse. Let's rewrite it and make it able to read the lengths of the legs from the console. 

Check out the editor window - this is how it looks now. 

The program asks the user twice for both legs, lengths, evaluates the hypotenuse and prints the result. 

Run it and try to input some negative values. 

The program - unfortunately - does not react to this obvious error. 

Let's ignore this weakness for now. We'll come back to it soon. 

Note that in the program that you can see in the editor, they hypo variable is used for only one purpose -t save the calculated value between the execution of the adjoining line of code. 

As the print() function accepts an expression as its argument, you can remove the variable from the code.

Just like this: 

leg_a = float(input("Input first leg length: "))
leg_b = float(input("Input second leg length: "))
print("Hypotenuse length is", (leg_a**2 + leg_b**2) ** .5)

String operators - introduction

It's time to return to these two arithmetic operators + and * 

We want to show you that they have a second function. They are able to do something more than just add and multiply

We've seen them in action where their arguments are numbers (floats or integers, it doesn't matter).

Now we're going to show you that they can handle strings, too, albeit in a very specific way. 

Concatenation

The + (plus) sign, when applied to two strings, becomes a concatenation operator:

string + string

It simply concatenates (glues) two strings into one. Of course, like its arithmetic sibling, it can be used more than once in one expression, and in such a context it behaves according to left-sided binding. 

In contrast to its arithmetic sibling, the concatenation operator is not commutative, i.e., "ab" + "ba" is not the same as "ba" + "ab"

Don't forget - if you want the + sign to be a concatenator, not an adder, you must ensure that both its arguments are strings.

You cannot mix types here. 

This simple program shows the + sign in its second use:

fnam = input("May I have your first name, please? ")
lnam = input("May I have your last name, please? ")
print("Thank you.")
print("\nYour name is " + fnam + " " + lnam + ".")

Note: using+ to concatenate strings lets you construct the output in a more precise way than with a pure print() function, even if enriched with the end= and sep= keyword arguments. 

Run the code and see if the output matches your predictions. 

More about input() and type casting

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

More about input() and type casting
No tags specified