This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Take two lists and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. | |
| a = [1,2,3,4,5,6,5,6,5,4,5,6] | |
| b = [5,6,7,8,9,0,9,78,5,6,45] | |
| c = [] | |
| for a1 in a: | |
| if a1 in b: | |
| c.append(a1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Create a program that asks the user for a number and then prints out a list of all the divisors of that number. (If you don’t know what a divisor is, it is a number that divides evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.) | |
| num = int(input("Please choose a number to divide: ")) | |
| numRange = range(1, num+1) | |
| for a in numRange: | |
| if num % a == 0: | |
| print(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Make a list and write a program that prints out all the elements of the list that are less than 5. | |
| #Extras: | |
| #Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list. | |
| #Write this in one line of Python. | |
| #Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user. | |
| b = [] | |
| a = [1,2,3,4,5,6,7,8,9,10] | |
| for a1 in a: | |
| if a1 < 5: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. | |
| #Extras: | |
| #If the number is a multiple of 4, print out a different message. | |
| #Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). | |
| #If check divides evenly into num, tell that to the user. If not, print a different appropriate message. | |
| num = int(input("Pick a number: ")) |