02.1 Simple Data Types - User Input
02.1 Simple Data Types - User Input
Multiple assignment
Constants
Comments
User input
Print function
SIMPLE DATA TYPES
NUMBERS
NUMBERS
Python treats numbers in several different ways, depending on how they’re being
used.
Integers:
You can add (+), subtract (-), multiply (*), and divide (/) integers in Python. Python uses two
multiplication symbols to represent exponents (**):
Examples:
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
The spacing in these examples has no effect on how Python evaluates the
expressions;
it simply helps you more quickly spot the operations that have priority when you’re reading
through the code.
FLOATS
Python calls any number with a decimal point a float and it refers to the fact that a
decimal point can appear at any position in a number.
You can use decimals without worrying about how they behave. Simply enter the
numbers you want to use, and Python will most likely do what you expect
Examples:
If you mix an integer and a float in any other operation, you’ll get a float as well:
>>> 1 + 2.0 >>> 2 * 3.0 >>> 3.0 ** 2
3.0 6.0 9.0
Python defaults to a float in any operation that uses a float, even if the output is a
whole number.
UNDERSCORES IN NUMBERS
When you’re writing long numbers, you can group digits using underscores to make
large numbers more readable:
>>> universe_age = 14_000_000_000
When you print a number that was defined using underscores, Python prints only the
digits:
>>> print(universe_age)
14000000000
Python ignores the underscores when storing these kinds of values. Even if you don’t
group the digits in threes, the value will still be unaffected.
To Python, 1000 is the same as 1_000, which is the same as 10_00.
MULTIPLE ASSIGNMENT
You can assign values to more than one variable using just a single line.
This can help shorten your programs and make them easier to read; you’ll use this
technique most often when initializing a set of numbers.
For example, here’s how you can initialize the variables x, y, and z:
>>> x, y, z = 5, 7, 9
>>> print(f'x = {x}\ny = {y}\nz = {z}')
You need to separate the variable names with commas, and do the same with the values, and
Python will assign each value to its respectively positioned variable.
As long as the number of values matches the number of variables, Python will match them up
correctly.
CONSTANTS
A constant is like a variable whose value stays the same throughout the life of a
program.
Python doesn’t have built-in constant types, but Python programmers use all capital
letters to indicate a variable should be treated as a constant and never be changed:
>>> MAX_CONNECTIONS = 5
Note that MAX_CONNECTIONS is just a variable which refers to a value of type int. It
has no other special properties (i.e. You can change the value of MAX_CONNECTIONS
constant by assigning new value to it).
EXERCISE 1
In the above example, Python ignores the first line and executes the second line.
USER INPUT & PRINTING
USER INPUT
Most programs are written to solve an end user’s problem. To do so, you usually
need to get some information from the user. To do this, you’ll use the input()
function.
How the input() Function Works?
The input() function pauses your program and waits for the user to enter some text.
The input() function takes one argument: the prompt, or instructions, that we want to display to
the user, so they know what to do.
Whatever you enter as input, input function convert it into a string; if you enter an integer value,
still input() function convert it into a string
USER INPUT
If you try to use the input as a number, you’ll get an error:
>>> age = input("How old are you? ")
How old are you? 21
❶ >>> age >= 18
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
❷ TypeError: unorderable types: str() >= int()
>>> type(variable_name)