Python PPT LESSON 7 - CONDITIONALS - LOOPS and INPUT PDF

Summary

This document details a Python presentation lesson on conditionals, loops, and input functions. It provides examples and explanations of these programming concepts.

Full Transcript

Lesson 7: Conditionals, Loops, and Input In this lesson, we are moving on to some of the more exciting concepts in computer programming. We will learn about conditionals, loops, and the input() function. Objectives After studying this lesson, you should be able to: define...

Lesson 7: Conditionals, Loops, and Input In this lesson, we are moving on to some of the more exciting concepts in computer programming. We will learn about conditionals, loops, and the input() function. Objectives After studying this lesson, you should be able to: define what conditionals and loops are; use conditionals and loops; and allow user input in your programs. IDLE Editor Click File > New File or Ctrl + N. To run a program, press F5. PyCharm Editor 1. Click the project root in the Project tool window > New > Python File. 2. Select Python file from the popup window, and then type the new filename. 3. Run the program (Shift-Alt-F10) What are Conditional Statements?  Conditional Statements are statements in Python that provide a choice for the control flow based on a condition. It means that the control flow of the Python program will be decided based on the outcome of the condition. Conditionals allow you to execute lines of code if particular conditions are met. Types of Conditional Statements in Python: 1. if Conditional Statement 2. if-else Conditional Statement 3. Nested if..else Conditional Statement 4. if-elif-else Conditional Statement 5. Ternary Expression Conditional Statement CONDITIONALS 1. if Statement: The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not. Syntax: if condition: # Statements to execute if # condition is true Here, the condition after evaluation will be either true or false. – if the condition is true then it will execute the block of statements below it otherwise not. NOTE: Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose. If statement, without indentation (will raise an error). CONDITIONALS 1. if Statement: if condition: # Statements to execute if # condition is true Example #1: OUTPUT: CONDITIONALS 1. if Statement: if condition: # Statements to execute if # condition is true Example #2: OUTPUT: CONDITIONALS 1. if Statement: if condition: # Statements to execute if # condition is true Example #3: OUTPUT: CONDITIONALS 1. if Statement: If you have only one statement to execute, you can put it on the same line as the if statement. Example: OUTPUT: CONDITIONALS 2.) if – else Statement: Syntax: if condition: # Statements to execute if # condition is true else: #Executes this block if #condition is false Here, if the condition is true then it will execute the block of statements below if otherwise, it will execute the block of statements below else. CONDITIONALS 2.) if – else Statement: Example: OUTPUT: How old are you? 15 You’re still a minor Program ended CONDITIONALS 3.) Nested if – else Statement: Nested if..else means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if – else statement is present and such type of statement is known as nested if statement. We can use one if or else if statement inside another if or else if statements. CONDITIONALS 3.) Nested if – else Statement: Example: OUTPUT: CONDITIONALS 3.) Nested if – else Statement: Example: OUTPUT: CONDITIONALS 3.) Nested if – else Statement: Example: OUTPUT: CONDITIONALS 4.) if-elif-else Conditional Statement: Syntax: The if statements are executed if (condition): from the top down. As soon as statement one of the conditions controlling elif (condition): the if is true, the statement statement associated with that if is executed,. and the rest of the ladder is. bypassed. If none of the else: statement conditions is true, then the final “else” statement will be executed. CONDITIONALS 4.) if-elif-else Conditional Statement: Example: Output: CONDITIONALS 4.) if-elif-else Conditional Statement: Example: Output: Nesting Conditions You can place other conditional statements within conditions. Example: Output We can further refine the code using the elif keyword so we can check for more than one condition. Example: Output CONDITIONALS 5. Ternary Operator /Expression: In Python, the Ternary Operator determines if a condition is true or false and then returns the appropriate value as the result. The ternary operator is useful in cases where we need to assign a value to a variable based on a simple condition, and we want to keep our code more concise — all in just one line of code. It’s convenient when we want to avoid writing multiple lines for a simple if-else condition. Like in simple if-else, the first option, the true_value will be executed when the condition provided in the expression is True. If the condition returns False, then false_value will be executed. Syntax: true_value if condition else false_value CONDITIONALS 5. Ternary Operator /Expression: Example: OUTPUT: CONDITIONALS 5. Ternary Operator /Expression: Example: OUTPUT: Logical Operators Example: Output: Comparing with if-elif-else There are times when you want to check a value against a group of values. One way to do this is to compare the value with each of the elements of the group. Say we want to check a letter if it is a vowel or not. We can manually set multiple conditions using if, elif, and else: Example: Output: Comparing with or operator A shorter version of this uses the or operator. Example: OUTPUT: Type any letter: a That is a vowel! Process finished with exit code 0 Comparing with the in Keyword As an alternative, we can also use the in keyword since it works for sequence values and collection data types. Example: OUTPUT: Type any letter: u That is a vowel! Process finished with exit code 0 Comparing with the in Keyword As an alternative, we can also use the in keyword since it works for sequence values and collection data types. Example: OUTPUT: Type any letter: s That is a consonant! Process finished with exit code 0 More Example of using the or operator Example: OUTPUT: Input total amount: Php 23100 Are you a senior citizen (y/n)? y You have a discount of Php 4620.00 So, you will only pay Php 18480.00 Loops Like most programming languages, Python provides flow control structures to implement repetition. These control structures, generally known as loops, allow us to write blocks of code that are executed at a given number of times, or simply executed until a certain condition is met. The advantage is that we need not write that block of code many times in the source code. Instead, we set up a loop to make the block execute as many times as required. Loops A simple example of this idea is a program that executes a given task, and then asks the user if they want to try again for a different input. Notice that we are not talking about an if statement to check if we should repeat the procedure — if the user chooses to try again, the idea is that the program will again ask the user if they want to try again, and so on, for as long as the user indicates. Loops Another example where you can apply a loop is for instance, if you want to print the statement “Welcome to Python world!” a hundred times. One way of doing this would be to use the print statement 100 times, which is not practical. The other way is by using the loops. You can simply put the print statement into a loop, which runs and prints the message as output a hundred times. Loops make a program more readable and enhance its efficiency by simplifying complex tasks. Loops What is a LOOP? LOOP - is a sequence of instructions that is repeated until a certain condition is reached. Types of Loops in Python: while loop for loop while Loop The while loop allows us to execute code repeatedly as long as the condition is true. Example # 1: Output Example # 2: Output while Loop The code i = i + 1 is called an incrementer. Every time the computer loops through the set of instructions, the value increments by one. Another way to write it is by using an assignment operator (i += 1). Assignment Operators while Loop Example: Output: while Loop Example: Output: for Loop We can also create loops using the for loop and in keywords. Let’s say we want to print each letter in the string “kerfuffle” Example: Output Iterating through Collections We can do this with strings because strings are sequences of characters. The characters can be referenced using offset. We can also do this with other collection data types. ❑Tuples Output ❑ Lists Output Iterating through Collections ❑Sets Example: Output: Iterating through Collections ❑Dictionaries - Using for and in on dictionaries loops through the keys Example: Output: Iterating through Collections ❑Dictionaries - Using for and in on dictionaries loops through the values() method Example: Output: Break and Continue The break keyword is used when you want to loop something indefinitely until a condition is met. Example: Output: Break and Continue The continue keyword skips running the code for an iteration. Example: Output: range() Function To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Example: Output: Note that range(6) is not the values of 0 to 6, but the values 0 to 5. range() Function The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6): Example: Output: range() Function The range() function can also take on three arguments/parameters: start - The start of the sequence stop - The end of the sequence step - How much the range increments Example: Output: input() Function The input() function allows us to show a prompt and have the user to provide some information. The function takes on a parameter for a prompt. Syntax Output Simple Guessing Game END OF SLIDES

Use Quizgecko on...
Browser
Browser