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

Summary Python for Beginners -part 2

Rating
-
Sold
-
Pages
13
Uploaded on
20-09-2024
Written in
2023/2024

this is the continuation of my python beginners course and this the last part and this improves your knowledge in python as a beginner and my next course is python advanced course before this u need to get all the main knowledge of python programming

Show more Read less
Institution
Module









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

Written for

Module

Document information

Uploaded on
September 20, 2024
Number of pages
13
Written in
2023/2024
Type
Summary

Subjects

Content preview

Python for Beginners -part 2
Looping with While Loops in Python

Introduction

While loops are used in Python to repeatedly execute a block of code as long as a certain condition is
true. This is different from a for loop, which iterates over a sequence (like a list or string) a specific
number of times.

While Loop Syntax

The basic syntax for a while loop is as follows:

while condition:

# code to be executed as long as condition is true

It is important to ensure that the condition will eventually become False, otherwise the loop will
continue indefinitely. This is known as an infinite loop and should be avoided.

Example:

Here's an example of using a while loop to print the numbers 1-5:

count = 1

while count <= 5:

print(count)

count += 1

This loop continues to execute as long as the variable count is less than or equal to 5. Each time
through the loop, it prints the current value of count and then increments it by 1.

Getting User Input and Performing Basic Calculations

While loops can also be used in conjunction with the input() function to get input from the user and
perform calculations based on that input.

Example:

Here's an example of getting a number from the user and printing whether it is even or odd:

num = int(input("Enter a number: "))

while num != 0:

if num % 2 == 0:

print("Even")

else:

print("Odd")

num = int(input("Enter a number: "))

, In this example, the loop continues to execute as long as the user enters a number that is not zero. If
the number is even, it prints "Even". If the number is odd, it prints "Odd".

Checking for Existence of Items in Lists with the In Operator

The in operator can be used in a while loop to check for the existence of an item in a list.

Example:

Here's an example of using the in operator to find the first prime number in a list:

numbers = [4, 5, 6, 7, 8]

i=0

while i < len(numbers):

if numbers[i] > 1:

is_prime = True

j=2

while j < numbers[i]:

if numbers[i] % j == 0:

is_prime = False

break

j += 1

if is_prime:

print(numbers[i])

break

i += 1

In this example, the loop iterates over each number in the list. If the number is greater than 1, it
checks if it is prime by dividing it by all numbers less than itself. If it is prime, it prints the number and
breaks out of the loop.

Conclusion

While loops are a powerful tool in Python that allow you to repeatedly execute a block of code as
long as a certain condition is true. They can be used for a variety of tasks, including getting user
input, performing calculations, and iterating over lists. It is important to ensure that the condition
will eventually become False and to use the break statement to exit the loop when necessary.

Here are the notes on the topic "Working with Lists in Python":

Working with Lists in Python

Lists are one of the fundamental data structures in Python. They are ordered collections of items,
which can be of different data types. Lists are written with square brackets [ ]. Here are some of the
main methods and operations that can be performed on lists in Python.
Free
Get access to the full document:
Download

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

Get to know the seller
Seller avatar
paulroshan653

Also available in package deal

Get to know the seller

Seller avatar
paulroshan653
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
19
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