Skip to content

nyu-python-programming/loops-exercises

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loop Problems

Create a separate file for each of the following problem sets, i.e. easy.py, medium.py, and difficult.py. Create separate functions for each problem. Create a main.py file that imports and runs each of the functions in each of the problem sets.

Easy

  1. Write a program that prints the numbers from 1 to 10 using a for loop.
  2. Write a program that prints the even numbers from 1 to 20 using a while loop.
  3. Write a program that calculates the sum of all numbers from 1 to 100 using a for loop.
  4. Write a program that prints the first 10 multiples of 3 using a while loop.
  5. Write a program that prints the factorial of a given number using a for loop. The factorial of a number is the product of all positive integers less than or equal to that number, e.g. 5! = 5 * 4 * 3 * 2 * 1 = 120.
  6. Write a program that prints the Fibonacci sequence up to a given number using a while loop. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, starting from 0 and 1, e.g. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ....
  7. Write a program that counts the number of vowels in a given string using a for loop. The vowel letters are a, e, i, o, and u.
  8. Write a program that checks if a given number is prime using a while loop. A prime number is a number greater than 1 that is only evenly divisible by 1 and itself.
  9. Write a program that prints the ASCII values of all uppercase letters using a for loop. You may use the built-in ord() function to convert any character to its ASCII value.
  10. Write a program that prints the reverse of a given string using a while loop.

Medium

  1. Write a program that finds the largest element in a given list using a for loop.
  2. Write a program that calculates the average of a list of numbers using a while loop. You are not allowed to use the built-in sum() function.
  3. Write a program that checks if a given string is a palindrome using a for loop. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, e.g. radar, level, 12321, mom, noon, civic, deified, racecar, madam, refer, repaper, rotor, sagas, solos, stats, tenet, wow, ...
  4. Write a program that prints the first n terms of the geometric sequence using a while loop. A geometric sequence is a sequence of numbers in which each term after the first is found by multiplying the previous term by a fixed, non-zero number called the common ratio, e.g. 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, ..., where the common ratio is 2.
  5. Write a program that finds the second largest element in a given list using a for loop.
  6. Write a program that calculates the factorial of a given number using a while loop.
  7. Write a program that checks if a given number is a perfect square using a for loop. A perfect square is a number that can be expressed as the product of two equal integers, e.g. 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ...
  8. Write a program that prints the sum of all prime numbers between 1 and 100 using a while loop.
  9. Write a program that counts the number of words in a given sentence using a for loop. Words can be separated by spaces, commas, periods, exclamation marks, question marks, etc. You may be interested in the built-in split() function, which splits a string into a list of words based on a delimiter. The delimiter is a space by default, but you can specify a different delimiter, e.g. split(','), split('.'), split('!'), split('?'), etc. You can even specify multiple delimiters, e.g. split(',.!?').
  10. Write a program that prints the common elements between two lists using a while loop.

Difficult

  1. Write a program that finds the prime factors of a given number using a for loop. A prime factor is a prime number that divides another number exactly without leaving a remainder, e.g. the prime factors of 12 are 2 and 3.
  2. Write a program that calculates the nth term of the Fibonacci sequence using a while loop.
  3. Write a program that checks if a given string is an anagram using a for loop.
  4. Write a program that prints the first n terms of the arithmetic sequence using a while loop. An arithmetic sequence is a sequence of numbers in which each term after the first is found by adding a fixed, non-zero number called the common difference to the previous term, e.g. 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, ..., where the common difference is 2.
  5. Write a program that finds the median of a given list of numbers using a for loop. The median is the middle value of a list of numbers when they are sorted in ascending order. If the list has an odd number of elements, the median is the middle element. If the list has an even number of elements, the median is the average of the two middle elements.
  6. Write a program that checks if a given number is a perfect number using a while loop. A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself, e.g. 6 is a perfect number because 1 + 2 + 3 = 6.
  7. Write a program that prints the sum of all digits in a given number using a for loop. For example, the sum of the digits in 12345 is 1 + 2 + 3 + 4 + 5 = 15.
  8. Write a program that finds the longest word in a given sentence using a while loop.
  9. Write a program that checks if a given string is a pangram using a for loop. A pangram is a sentence that contains every letter of the alphabet at least once, e.g. The quick brown fox jumps over the lazy dog.
  10. Write a program that prints the prime numbers between 1 and 1000 using a while loop.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages