100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Samenvatting

Summary Python for Beginners -part 2

Beoordeling
-
Verkocht
-
Pagina's
13
Geüpload op
20-09-2024
Geschreven 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

Meer zien Lees minder
Instelling
Vak









Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Vak

Documentinformatie

Geüpload op
20 september 2024
Aantal pagina's
13
Geschreven in
2023/2024
Type
Samenvatting

Onderwerpen

Voorbeeld van de inhoud

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.
Gratis
Krijg toegang tot het volledige document:
Downloaden

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
paulroshan653

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
paulroshan653
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
0
Lid sinds
1 jaar
Aantal volgers
0
Documenten
19
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen