PowerPoint Presentation
Basics in programming
Lectures 5
Decission making
Input and Output
The Boolean type
Exception handling
Python has the usual comparison operations: ==, !=, <, <=, >, >=.
Unlike Java and C, == is overloaded to work correctly with strings.
Decission making
Decission making
The boolean operators are the spelled out words:
*and*
*or*
*not*
(Python does not use the C-style && || !).
Decission making
AND
The expression A and B is true if both A is true and B is true, and false if either is false.
(For example, you get wet if it rains and you forgot your umbrella.)
Decission making
AND
Decission making
OR
The expression A or B is true if either A is true or B is true, and false if both are false.
(For example, school is closed if it is a holiday or it is a weekend.)
Decission making
OR
Decission making
NOT
The expression not A is true if A is false, and false if A is true. (For example, you are hungry if you have noteaten lunch.)
Decission making
NOT
Decission making
Decission making
Sometimes, you may want your program to do one thing if a condition is true and something else if a condition is false.
Notice that the if statement does something only when the condition evaluates to true and does not do anything otherwise.
If you want one thing to happen when a condition is true and another to happen if the condition is false then you need to use an if-else statement.
Decission making
Decission making
Again, indentation is very important. The else keyword must line up with the if statement to be properly paired with the if statement by Python.
Decission making
Again, indentation is very important. The else keyword must line up with the if statement to be properly paired with the if statement by Python.
Example:
Crate if-else decission making which uses:
AND
OR
NOT
Decission making
The need to select between several choices presents itself often enough that Python has a special form of the if statement to handle this.
It is the if-elif statement. In this statement, one, and only one, alternative is chosen.
The first alternative whose condition evaluates to True is the code that will be executed. All other alternatives are ignored.
Decission making
Decission making
Example:
Make program that uses if-elif-else decission making
Decission making
Getting input
To get input from the user you can use the input() function.
When the input() function is called the program stops running the program, prompts the user to enter something at the keyboard by printing a string called the prompt to the screen, and then waits for the user to press the Enter key
Getting input
The type of value read by Python is always a string
Getting input from user
If you want to convert input into number you need to use int() or float() functions
Formatting output
When printing, we may print as many items as we like on one line by separating each item by a comma.
Each time a comma appears between items in a print statement, a space appears in the output.
name = ”Jari”
print(“I hope that,”, name, “is feeling well today.”)
Formatting output
To print the contents of variables without spaces appearing between the items, the variables must be converted to strings and string concatenation can be used.
The + operator adds numbers together, but it also concatenates strings.
For the correct + operator to be called, each item must first be converted to a string before concatenation can be performed.
Formatting output
num1 = 5
num2 = 10
print(str(num1)+”*”+str(num2)+”=”+str(num1+num2))
String %
The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point), and the matching values in a tuple on the right
(a tuple is made of values separated by commas, typically grouped inside parentheses)
Formatting output
% operator
text = “%d little pigs come out, or I’ll %s, and I’ll %s, and I’ll blow your %s down.” % (3, ‘huff’, ‘puff’, ‘house’)
Formatting output
Formatting output
Example of output formatting
Formatting output
Conditions in if statements evaluate to True or False.
One of the types of values in Python is called bool which is short for Boolean.
In an if statement the condition evaluates to true or false. The Boolean value of the condition decides which branch is to be executed.
The Boolean type
You can use Boolean type to store result of the comparison and then use this variable in of clause.
result = age > 18 and age < 20 If result then: print(”correct age”) The Boolean type In Python, real numbers or floats are represented using eight bytes. That means that 264 different real numbers can be represented. This is a lot of real numbers, but not enough. Since there are infinitely many real numbers between any two real numbers, computers will never be able to represent all of them. Comparing Floats for Equality Because floats are only approximations of real numbers, there is some round-off error expected when dealing with real numbers in a program. Generally this round-off error is small and is not much of a problem unless you are comparing two real numbers for equality. If you need to do this then you need to subtract the two numbers and see if the difference is insignificant since the two numbers may be slightly different Comparing Floats for Equality This program compares a guess with the result of dividing two floats and tells you if you are correct or not. Comparing Floats for Equality Comparing Floats for Equality Sometimes things go wrong in a program and it is out of your control. For instance, if the user does not enter the proper input an error may occur in your program. Python includes exception handling so programmers can handle errors like this. Exception handling Use exception handling alway when there is possiblity that thing could go wrong. Exception handling Exception handling Exception handling the Exception is optional. That’s what the square brackets (i.e. [ ]) mean. They mean the exception is optional in this case. If the exception is omitted then any exception is caught. A try-except block may monitor for any exception or just a certain exception. There are many possible exceptions that might be caught. For instance, a ValueError exception occurs when you try to convert an invalid value to an integer. A ZeroDivisionError exception occurs when you try to divide by zero. Exception handling Exception handling Memory management & garbage collection https://realpython.com/python-memory-management/ http://patshaughnessy.net/2013/10/24/visualizing-garbage-collection-in-ruby-and-python Excercises 4. Write a program that asks user to input two numbers. If product of numbers is divisible with number 7 program prints: ”Great!” else it print ”Bad luck!”. Write program that check if person can run to be chosen in Finnish parliament. Ask user to input name and nationality. If user is age 18 or more and nationality is Finnish print YES otherwise NO /docProps/thumbnail.jpeg