Shawn's Programming Portfolio

20 Questions.
Python Programming Language.
List made by Adriann.
Let me know what you think!

Basics

1. Write a program that prints ‘Hello World’ to the screen.

print("hello world") 
  
hello world

2. Write a program that asks the user for their name and greets them with their name.

name = input('Enter your name:')
print('Hello, ' + name)

Enter your name: shawn
Hello, Shawn

3. Modify the previous program such that only the users Alice and Bob are greeted with their names.

name = input('Enter your name:')
if name == "bob" or name == "alice":
    print('Hello, ' + name) 
    
Enter your name: shawn
Enter your name: alice
Hello, alice

4. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.

x = int(input("Enter a number: "))
i = 0
count = 0
while i < x + 1:
    print(count)
    count += i
    i += 1
    
Enter a number: 5
0
1
3
6
10
15

5. Modify the previous program such that only multiples of three or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17

x = int(input("Enter a number: "))
i = 0
count = 0
while i < x + 1:
    i += 1
    if i % 3 == 0 or i % 5 == 0:
        print(count)
        count += i

Enter a number: 15
0
3
8
14
23
33
45

6. Write a program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n.

x = int(input("Enter a number: "))
choice = input("What would you like to compute? The sum (1) or the product (2): ")
i = 1
count = 1
if choice == "1":
    while i < x + 1:
        print(count)
        i += 1
        count += i
if choice == "2":
    while i < x + 1:
        print(count)
        count = count * i
        i += 1
        
Enter a number: 5
What would you like to compute? The sum (1) or the product (2): 1
1
3
6
10
15
Enter a number: 6
What would you like to compute? The sum (1) or the product (2): 2
1
2
6
24
120

7. Write a program that prints a multiplication table for numbers up to 12.

i = 1
while i <= 12:
    n = 1
    while n <= 12:
        print("{} ".format(n*i), end='')
        n += 1
    i += 1
    print()
    
1 2 3 4 5 6 7 8 9 10 11 12 
2 4 6 8 10 12 14 16 18 20 22 24 
3 6 9 12 15 18 21 24 27 30 33 36 
4 8 12 16 20 24 28 32 36 40 44 48 
5 10 15 20 25 30 35 40 45 50 55 60 
6 12 18 24 30 36 42 48 54 60 66 72 
7 14 21 28 35 42 49 56 63 70 77 84 
8 16 24 32 40 48 56 64 72 80 88 96 
9 18 27 36 45 54 63 72 81 90 99 108 
10 20 30 40 50 60 70 80 90 100 110 120 
11 22 33 44 55 66 77 88 99 110 121 132 
12 24 36 48 60 72 84 96 108 120 132 144

8. Write a program that prints all prime numbers.

primeNum = []
for i in range(2, 100):
    isPrime = True
    for n in range(2, int(i ** 0.5) + 1):
        if i % n == 0:
            isPrime = False
            break

    if isPrime:
        primeNum.append(i)

print(primeNum)

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

9. Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed.

secret = 69

while True:
    guess = int(input("Please guess a number(0-100): "))
    if guess == secret:
        print("You've guessed the secret number!")
        break
    if guess > secret:
        print("Go lower")
    if guess < secret:
        print("Go higher")

Please guess a number(0-100): 50
Go higher
Please guess a number(0-100): 75
Go lower
Please guess a number(0-100): 65
Go higher
Please guess a number(0-100): 70
Go lower
Please guess a number(0-100): 69
You've guessed the secret number!

    

10. Write a program that prints the next 20 leap years.

year = 2019
count = 0
while count < 20:
    if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
        print(year)
        count += 1
    year += 1

2020
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080
2084
2088
2092
2096

11. Write a program that prints an Easter Egg.

yup = True
while yup == True:
    choice = input("Please enter a special character(@#$%&): ")
    if choice == "@" or choice == "#" or choice == "$" or choice == "%" or choice == "&":
        print("   __   ")
        print("  /" + choice + choice + "\  ")
        print(" /" + choice + choice + choice + choice + "\  ")
        print(" |" + choice + choice + choice + choice + "| ")
        print(" \____/")
        break
    else:
        print("Please enter one of the accepted characters.")

Please enter a special character(@#$%&): &
  /&&\  
 /&&&&\  
 |&&&&| 
 \____/

Lists & Strings

12. Write a function that returns the largest element in a list.

myList = [1,2,3,4,5,6,7,8,9]
print(max(myList))

9 

13. Write function that reverses a list, preferably in place.

myList = [1,2,3,4,5,6,7,8,9]
myList.reverse()
print(myList)
 
 
[9, 8, 7, 6, 5, 4, 3, 2, 1]

14. Write a function that checks whether an element occurs in a list.

myList = [0,1,2,3,4,5,6,7,8,9]

def locate(int, list):
    for i in list:
        if i == int:
            print("it is in the list")
            break
    else:
        print("it is not in the list")


it is in the list
it is not in the list

15. Write a function that returns the elements on odd positions in a list.

myList = [0,1,2,3,4,5,6,7,8,9]

def oddPrint(list):
    for i in list:
        if i % 2 != 0:
            print(list[i])

1
3
5
7
9

16. Write a function that computes the running total of a list.

myList = [0,1,2,3,4,5,6,7,8,9]

def listCount(list):
    count = 0
    for i in list:
        count += i
    print(count)

45

17. Write a function that tests whether a string is a palindrome.

def isPalindrome(string):
    string1 = string.lower()
    string2 = string.lower() [::-1]
    if string1 == string2:
        print("Yes, it is a Palindrome")
    else:
        print("No, it is not a Palindrome")


Yes, it is a Palindrome
No, it is not a Palindrome

18. Write three functions that compute the sum of the numbers in a list: using a for-loop, a while-loop and recursion.

myList = [0,1,2,3,4,5,6,7,8,9]

def sumForLoop(list):
    count = 0
    for i in list:
        count += i
    print(count)

def sumWhileLoop(list):
    count = 0
    i = 0
    while i < len(list):
        count += i
        i += 1
    print(count)

def recursion(list, int):
    if int >= len(list):
        return 0
    print(list[int] + recursion(list, int + 1))



45
45
45

19. Write a function that concatenates two lists.

list1 = [0,1,2,3,4]
list2 = [5,6,7,8,9]
list3 = list1 + list2

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

20. Write a function that combines two lists by alternatingly taking elements.

list1 = [0,1,2,3,4]
list2 = [5,6,7,8,9]
list3 = [None]*(len(list1)+len(list2))
list3[::2] = list1
list3[1::2] = list2

[0, 5, 1, 6, 2, 7, 3, 8, 4, 9]




←Go back to shawnbeaton.com