100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4,6 TrustPilot
logo-home
Resumen

Summary Python for Beginners -part 2

Puntuación
-
Vendido
-
Páginas
13
Subido en
20-09-2024
Escrito en
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

Mostrar más Leer menos
Institución
Grado

Vista previa del contenido

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.

Escuela, estudio y materia

Grado

Información del documento

Subido en
20 de septiembre de 2024
Número de páginas
13
Escrito en
2023/2024
Tipo
Resumen

Temas

Gratis
Accede al documento completo:
Descarga

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
paulroshan653

Documento también disponible en un lote

Conoce al vendedor

Seller avatar
paulroshan653
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
0
Miembro desde
1 año
Número de seguidores
0
Documentos
19
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes