033 if else statements tủ tài liệu training

4 25 0
033 if else statements tủ tài liệu training

Đang tải... (xem toàn văn)

Thông tin tài liệu

If Else Statements In this lesson, we will learn to make decisions in a Python program using different forms of if else statement Python's Working Logic - Code Indentation In Python, each line of code is executed respectively until our program ends Let's see this with a simple example below: Before we start to the conditional statements, let's look at how to a Python program runs if you want When a Python program runs, it starts from the top to the bottom Each line and each operation is executed rescpectively and step by step And if there is no code to be run, our Python program ends In [1]: a = b = c = print(a+b+c) In the program above each line of code is executed respectively until our program ends And when it finished, it is printed on the screen the sum of values as you can see So, this is our the essential logic of working Python programs But, the structure of every Python programs that we will write in the future may not be that simple Python programs are actually consist of block of codes generally So, what is a block of code? You can think a block of code as a unit that consist of more than one code lines working for a specific task in a particular domain From now on, usually we are going to write our Python codes within block of codes anymore To indicate a block of code in Python, you must indent each line of the block by the same amount Python differentiates the block of codes thanks to the indents This block of code in our example if-statement is indented four spaces, which is a typical amount of indentation for Python In [2]: # Simple Password Checking Program password = "omer123" # Block if password == "omer123": print('Logged in ') # Block print('All done!') # Block Logged in All done! The Complete Python Programming Course: (Beginner to Advanced) In above, they were printed "Logged in" and "All done" So, print function is executed two times although their blocks are different All the codes of Block and Block are being executed In [3]: # Simple Password Checking Program password = "omer123" # Block if password == "alice": print('Logging on ') # Block print('All done!') # Block All done! Since in our if condtion password's value is "omer123" and it is not equal to "alice" So, our if condition is not satisfied Therefore, it's not printed "Logging on message" Python Conditional Statements Now, we're ready to learn conditional statements in Python Conditional statements is required when we want to execute a code only if a certain condition is satisfied Let’s start with if statements if Statement if statements in Python are very simple If statement allows us to check a a condition that we need in anywhere in our programs It works only condition is True Here is our structure of if statements if (condition): something #if statement body The body of the if statement is indicated by the indentation Body starts with an indentation and the first unindented line marks the end Let's make an example: In [4]: # Checks whether a number is positive or negative num = int(input("Please enter a number: ")) if num > 0: print(num, "is a positive number.") else: print(num, "is a negative number.") Please enter a number: 2 is a positive number The Complete Python Programming Course: (Beginner to Advanced) else Statement else block works in case of the if statement is not satisfied The structure of else block is quite similar with if statement else: something #else block body There is nothing to write after the else statement, because it works in case of the if statement is not satisfied Let's make an another example In [5]: # age control age = int(input("Please enter your age: ")) if (age < 18): print("You're not allowed to enter this place!") else: print("Welcome to the club!") Please enter your age: 18 Welcome to the club! In [6]: # Simple Password Checking Program password = "omer" # Block if password == "alice": print('Logging on ') # Block else: print('Incorrect password.') # Block print('All done!') # Block Incorrect password All done! We can write an if block without an else block, but, we can not write else block without an if block If we want to write an else block, we have to write an if block else block is always written after if statement In [7]: if < 2: print("Hello world!") Hello world! In [8]: else: print("Hello world!") File "", line else: ^ SyntaxError: invalid syntax The Complete Python Programming Course: (Beginner to Advanced) This is the core logic of the conditional statements That's all for this lesson We're going to continue to see other conditional statements in next lesson In [ ]: The Complete Python Programming Course: (Beginner to Advanced) ... conditional statements in Python Conditional statements is required when we want to execute a code only if a certain condition is satisfied Let’s start with if statements if Statement if statements. .. without an if block If we want to write an else block, we have to write an if block else block is always written after if statement In [7]: if < 2: print("Hello world!") Hello world! In [8]: else: ... (Beginner to Advanced) else Statement else block works in case of the if statement is not satisfied The structure of else block is quite similar with if statement else: something #else block body

Ngày đăng: 17/11/2019, 07:34

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan