101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Refreshing Python Basics

    Understanding Conditionals and Loops in Python

    general-purpose programming language

    General-purpose programming language.

    Python, like any other programming language, uses conditional statements and loops to control the flow of execution. This article will provide a comprehensive overview of these fundamental concepts.

    Conditional Statements

    Conditional statements in Python are used to make decisions based on certain conditions. The primary conditional statements in Python are if, else, and elif.

    If Statement

    The if statement is used to test a specific condition. If the condition is true, the block of code under the if statement will execute.

    x = 10 if x > 5: print("x is greater than 5")

    Else Statement

    The else statement is used to execute a block of code if the condition in the if statement is false.

    x = 2 if x > 5: print("x is greater than 5") else: print("x is not greater than 5")

    Elif Statement

    The elif statement, short for else if, is used to check multiple conditions.

    x = 5 if x > 5: print("x is greater than 5") elif x == 5: print("x is equal to 5") else: print("x is less than 5")

    Loops

    Loops in Python are used to repeatedly execute a block of code. Python provides two types of loops: for and while.

    For Loop

    The for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range) or other iterable objects.

    for i in range(5): print(i)

    While Loop

    The while loop in Python is used to execute a block of code repeatedly as long as a given condition is true.

    i = 0 while i < 5: print(i) i += 1

    Loop Control Statements

    Loop control statements change the execution of the loop from its normal sequence. Python supports the following control statements.

    Break Statement

    The break statement is used to terminate the loop prematurely when a certain condition is met.

    for i in range(5): if i == 3: break print(i)

    Continue Statement

    The continue statement is used to skip the rest of the code inside the current loop for the current iteration only.

    for i in range(5): if i == 3: continue print(i)

    Pass Statement

    The pass statement is a null operation. Nothing happens when it executes. It is used as a placeholder.

    for i in range(5): if i == 3: pass print(i)

    Conclusion

    Understanding conditional statements and loops is fundamental to programming in Python. They allow you to control the flow of your programs and make them more efficient and effective. Practice these concepts with different examples to get a good grasp of them.

    Test me
    Practical exercise
    Further reading

    Hey there, any questions I can help with?

    Sign in to chat
    Next up: Understanding Class and Objects