Python if Statements and Indentation

Day 10/60 Continuing Chapter 2: Flow Control Continuing Topic l - Making Decisions 2. Code blocks i⁠f⁠ statements don't decide on skipping or running the entire code. They only make decisions about a code block. We use an indentation of two spaces to highlight code blocks, like indenting this display statement. Indentation refers to the space between the code and the code editor's margin. A code block can be more than a one-liner. All lines with the same indentation belong to the same code block. We can see it here when we use  p⁠r⁠i⁠n⁠t⁠(⁠)⁠ to add one more line at the beginning. 🧩Code if True: print ("Look at me!") print("I'm a code block") 🖥️Output Look at me! I'm a code block If the indentation is incorrect, the computer can't understand your code. This leads to an  I⁠n⁠d⁠e⁠n⁠t⁠a⁠t⁠i⁠o⁠n⁠E⁠r⁠r⁠o⁠r⁠ showing up in the console. Instead of using the boolean  T⁠r⁠u⁠e⁠ , we can save it in a variable like  g⁠r⁠e⁠e⁠t⁠ and use it as the condition. 🧩Code greet = True if greet: print("Hello!") 🖥️Output Hello! Using a variable like  g⁠r⁠e⁠e⁠t⁠ set to  F⁠a⁠l⁠s⁠e⁠ allows us to skip code like the display statement. 🧩Code greet = False if greet: print("Hello!") 🖥️Output is empty 🧠Challenge of the day What is the output of this code? 🧩Code if True: print ("Look at me!") print("I'm a code block") #python #ai #programming #bigtech

To view or add a comment, sign in

Explore content categories