02.1 Simple Data Types - User Input

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

SIMPLE DATA TYPES & USER INPUT

13777 SPECIAL TOPICS IN HEALTH TECHNOLOGY


DR. AHMAD ALTAMIMI
OUTLINES

Simple Data Types: Numbers

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 >>> 2 * 3 >>> 3 ** 2


5 6 9
>>> 3 - 2 >>> 3 / 2 >>> 3 ** 3
1 1.5 27
The precedence of arithmetic operators is (from highest to
ARITHMETIC lowest)
OPERATORS Operators Meaning

 Python supports the () Parentheses

order of operations ** Exponent


too, so you can use +x, -x, ~x Unary plus, Unary minus, Bitwise NOT
multiple operations in
one expression. *, /, //, % Multiplication, Division, Floor division, Modulus

 You can also use +, - Addition, Subtraction


parentheses to modify =, !=, >, >=, <, <=,
Comparisons, Identity, Membership operators
the order of operations is, is not, in, not in
so Python can evaluate Not Logical NOT
your expression in the
order you specify. and Logical AND
or Logical OR
ARITHMETIC OPERATORS
 Example:

>>> 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:

>>> 0.1 + 0.1 >>> 2 * 0.1


0.2 0.2
>>> 0.2 + 0.2 >>> 2 * 0.2
0.4 0.4
FLOATS
 But be aware that you can sometimes get an arbitrary number of decimal places in
your answer.
 Examples: >>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
 This happens in all languages and is of little concern. Python tries to find a way to
represent the result as precisely as possible, which is sometimes difficult given how
computers have to represent numbers internally. Just ignore the extra decimal places
for now.
INTEGERS AND FLOATS
 When you divide any two numbers, even if they are integers that result in a whole
number, you’ll always get a float:
>>> 4/2
2.0

 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

 Write a separate program to accomplish this exercise.


Save the program with a filename number_eight.py.
 Write addition, subtraction, multiplication, and division
operations that each result in the number 8.
 Be sure to enclose your operations in print() calls to see the
results. You should create four lines that look like this:
print(5+3)
print(?-?)
 Your output should simply be four lines with the number 8
appearing once on each line.
COMMENTS
 As your programs become longer and more complicated, you should add notes
within your programs that describe your overall approach to the problem you’re
solving. A comment allows you to write notes in English within your programs.
 How Do You Write Comments?
 In Python, the hash mark (#) indicates a comment. Anything following a hash mark in
your code is ignored by the Python interpreter. For example:
# Say hello to everyone.
print("Hello Python people!")

 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()

 When you try to use the input to do a numerical comparison ❶, Python


produces an error because it can’t compare a string to an integer: the string
'21' that’s assigned to age can’t be compared to the numerical value 18 ❷
 We can resolve this issue by using the typecasting functions, which tells Python to treat the input
as a numerical value.
USER INPUT
 You can confirm this by calling type().

>>> type(variable_name)

 How to get an Integer or Float as the User Input?


 You can type cast user input to integer, float or string by using typecasting functions that is int(),
float(), and str().
>>> x = int(UserInputText)
>>> y = float(UserInputText)
>>> z = str(UserInputText)
PRINT FUNCTION
 Calls syntax to the print() function have the following form in Python 3.3 and later:
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout][, flush=False])
 In this formal notation, items in square brackets are optional and may be omitted in
a given call, and values after = give argument defaults.
 sep: is a string inserted between each object’s text.
 end: is a string added at the end of the printed text.
 file: specifies the file, standard stream, or other file-like object to which the text will be sent. But,
real files should be already opened for output.
 flush: allows prints to mandate that their text be flushed through the output stream immediately
to any waiting recipients.
USER INPUT
USER INPUT & PRINTING
EXERCISE 2

 Write a separate program to accomplish this exercise.


Save the program with a filename rental_car.py.
 Asks the user what kind of rental car they would like?
 Then, print a message about that car, such as “Let me
see if I can find you a BMW.”
EXERCISE 3

 Write a separate program to accomplish this exercise.


Save the program with a filename
multiples_itself.py.
 Asks the user for a number, and then print the
number multiplied by itself.

You might also like