100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Other

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

Rating
-
Sold
-
Pages
7
Uploaded on
24-01-2025
Written in
2024/2025

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.

Show more Read less
Institution
Course

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

Written for

Institution
Course

Document information

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

Subjects

$6.39
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Get to know the seller

Seller avatar
rileyclover179 US
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

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

Frequently asked questions