Skip to content

Instantly share code, notes, and snippets.

@ehedaoo
ehedaoo / Practice1.py
Last active April 30, 2017 12:28
Create a 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.
# Create a 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 = input("Hello! Please enter your name: ")
print("Hello " + name)
age = int(input("Enter your age: "))
year_now = datetime.datetime.now()
#print(year_now.year)
print("You will turn 100 in " + str(int(100-age) + int(year_now.year)))
@ehedaoo
ehedaoo / Practice1a.py
Created April 30, 2017 13:44
Create a 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. Add on to the previous program by asking the user for another number and printing out that many copies of the previous message.
# Create a 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.
# Add on to the previous program by asking the user for another number
# and printing out that many copies of the previous message.
import datetime
def call1():
print("You will turn 100 in " + str(int(100-age) + int(year_now.year)))
@ehedaoo
ehedaoo / Practice2.py
Created April 30, 2017 17:49
Ask the user for a number. Depending on whether the number is even or odd. print out an appropriate message to the user.
# Ask the user for a number. Depending on whether the number is even or odd.
# print out an appropriate message to the user.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Your number is even")
else:
print("Your number is odd")
@ehedaoo
ehedaoo / Practice2a.py
Created April 30, 2017 17:51
Ask the user for a number. Depending on whether the number is even or odd. print out an appropriate message to the user. If the number is a multiple of 4, print out a different message.
# Ask the user for a number. Depending on whether the number is even or odd.
# print out an appropriate message to the user.
# If the number is a multiple of 4, print out a different message.
num = int(input("Enter a number: "))
if num % 2 == 0:
if num % 4 == 0:
print("Your number is a multiple of 4")
else:
print("Your number is even")
@ehedaoo
ehedaoo / Practice2b.py
Created April 30, 2017 18:07
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.
# 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("Enter a number: "))
check = int(input("Enter a number to divide by: "))
if num % check == 0:
print("Yusssss")
else:
@ehedaoo
ehedaoo / Practice3.py
Created May 1, 2017 05:48
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.
# 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 = []
num = 5
for i in a:
if i<num:
@ehedaoo
ehedaoo / Practice3a.py
Created May 1, 2017 06:09
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.
# 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.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
new_list = []
num = input("Enter a number to be compared with: ")
for i in a:
if i<num:
new_list.append(i)
@ehedaoo
ehedaoo / List1b.py
Created May 2, 2017 05:10
Write a Python program to sum all the items in a list. using function
# Write a Python program to sum all the items in a list.
# use function
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def sum_list(a):
sum_test = 0
for i in a:
sum_test += i
@ehedaoo
ehedaoo / List1c.py
Created May 2, 2017 07:04
Write a Python program to multiplies all the items in a list.
# Write a Python program to multiplies all the items in a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def multiply_items(a):
mult = 1
for i in a:
mult = mult*i
return mult
@ehedaoo
ehedaoo / List1e.py
Created May 2, 2017 09:46
Write a Python program to get the largest number from a list. Using function
# Write a Python program to get the largest number from a list.
# Using function
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def max_num(a):
flag = 0
for i in a:
if i > flag:
flag = i