100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary Python for Beginners -part 2 $2.99   Add to cart

Summary

Summary Python for Beginners -part 2

 0 view  0 purchase
  • Course
  • Institution

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

Preview 2 out of 13  pages

  • September 20, 2024
  • 13
  • 2023/2024
  • Summary
avatar-seller
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.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller paulroshan653. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $2.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

76658 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$2.99
  • (0)
  Add to cart