This file contains 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
'''Write a program that asks the user how many Fibonnaci numbers to generate and then | |
generates them. Take this opportunity to think about how you can use functions. | |
Make sure to ask the user to enter the number of numbers in the sequence to generate''' | |
n = int(input("Enter the number of fibonacci sequence numbers to be generated")) | |
#Using Recursion | |
def generate_fib(num): | |
if(num==0): | |
return 0 | |
elif(num==1): | |
return 1 |
This file contains 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
a = [5, 10, 15, 20, 25] | |
length = len(a) | |
c=[] | |
def print_new_list(b): | |
c.append(b[0]) | |
c.append(b[length-1]) | |
print_new_list(a) | |
print(c) |
This file contains 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
'''Let's say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. | |
Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.''' | |
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
print(list(filter(lambda x:(x%2==0),a))) |
This file contains 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 string and print out whether this string is a palindrome or not. | |
(A palindrome is a string that reads the same forwards and backwards.)''' | |
s = str(input("Enter a string to check whether this is palindrome or not")) | |
match = True | |
count=-1 | |
for c in s: | |
if c == s[count]: | |
count-=1 | |
continue |
This file contains 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, say for example these two: | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | |
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. | |
Extras: | |
Randomly generate two lists to test this | |
Write this in one line of Python (don't worry if you can't figure this out at this point - we'll get to it soon)''' |
This file contains 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
''' To print all divisors of a number''' | |
num = int(input("Enter a number to print all the divisors")) | |
new_list = [] | |
for x in range(1,num+1): | |
if(num%x==0): | |
new_list.append(x) | |
else: | |
continue |
This file contains 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 a list, say for example this one: | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
and write a program that prints out all the elements of the list that are less than 5.''' | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
new_list = [] | |
for i in a: | |
if(i >= 5): |
This file contains 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.''' | |
num1 = int(input("Enter a number")) | |
num2 = int(input("Enter one more number")) | |
if num1%2==0: | |
if num1%4 ==0: | |
print("Your number is both an even number and also multiple of 4") | |
else: | |
print("You entered an even number") |
This file contains 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
'''Program that asks the user to enter their name and their age. Print out a message addressed to them that tells them | |
the year that they will turn 100 years old.''' | |
import datetime | |
name=str(input("Please enter your name")) | |
age=int(input("Please enter your age if you are aged below 100")) | |
year_turning_100_years = datetime.datetime.now().year + (100-age) | |
print("Hello %s, You shall be turning 100 years old in %d:" %(name,year_turning_100_years)) |