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

Lecture notes Computer Science

Rating
-
Sold
-
Pages
2
Uploaded on
08-08-2023
Written in
2021/2022

If and Else statements are used for decision-making in programming. They allow your code to execute different blocks of code based on specific conditions. Remember to properly indent your code to define the scope of each block. Python uses indentation (typically four spaces) to group statements together. This is crucial for the correct execution of your program.

Show more Read less








Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
August 8, 2023
Number of pages
2
Written in
2021/2022
Type
Lecture notes
Professor(s)
Mark
Contains
All classes

Subjects

Content preview

How to use If & Else statements in python?



If and Else statements are used for decision-making in programming. They allow your code to
execute different blocks of code based on specific conditions.

The basic syntax of an If statement in Python is as follows:

```python
if condition:
# Code block to execute if the condition is True
```

For example, let's check if a number is greater than 10 and print a message accordingly:

```python
number = 15

if number > 10:
print("The number is greater than 10!")
```

In this example, since the condition `number > 10` is True (15 is indeed greater than 10), the
message "The number is greater than 10!" will be printed.


You can also use an Else statement to specify what code block to execute when the condition in
the If statement is False:

```python
number = 5

if number > 10:
print("The number is greater than 10!")
else:
print("The number is less than or equal to 10.")
```

In this case, the condition `number > 10` is False (5 is not greater than 10), so the message
"The number is less than or equal to 10." will be printed.
£5.71
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
sauravpatel1

Get to know the seller

Seller avatar
sauravpatel1 Halliford St David’s
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
2 year
Number of followers
0
Documents
1
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 exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight 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 smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions