• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 7 pages
Other

Control Structures in Python: A Beginner’s Guide with Examples

Document preview thumbnail
Preview 2 out of 7 pages

This document explains control structures in Python, including decision-making with if-else statements, looping with for and while, and handling nested conditions. Learn how to control program flow with practical examples. Perfect for first-year Computer Science students starting with Python.

Content preview

Control Structures

1. If-Else Statements
 The if-else statement allows you to make decisions in your code. If a
condition is true, the code inside the if block is executed; otherwise, the
code inside the else block is executed.

Syntax:

if condition:
# Execute this block if the condition is True
else:
# Execute this block if the condition is False

Example:

age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

Output: You are an adult.

Elif (Else If):

 Sometimes you need to check multiple conditions. In such cases, you can
use the elif (else if) statement to test multiple conditions.

Syntax:

if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:

, # Code if neither condition1 nor condition2 is True

Example:

age = 15
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")

Output: You are a teenager.



2. Logical Operators
 Logical operators allow you to combine multiple conditions to make more
complex decisions.
o and: Returns True if both conditions are True.
o or: Returns True if at least one condition is True.
o not: Reverses the truth value of a condition.


Example:

x=5
y = 10
if x > 3 and y < 15:
print("Both conditions are True")
else:
print("At least one condition is False")

Output: Both conditions are True

Document information

Uploaded on
January 24, 2025
Number of pages
7
Written in
2024/2025
Type
Other
Person
Unknown
$6.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions