Python Exercises 1 — Basic

print(“Twinkle, twinkle, little star, \n\tHow I wonder what you are! \n\t\tUp above the world so high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star, \n\tHow I wonder what you are”)
from platform import python_version
print(python_version())
from datetime import datetimenow = datetime.now() # Current date and timedate_time = now.strftime(“%Y-%m-%d %H:%M:%S”)print(“Current date and time :\n”, date_time)
import mathprint(“Enter the radius:”)
r = float(input())
a = math.pi * (r ** 2.0)
print(“r = “, (r))
print(“Area = “, (a))
print(“Enter your first name:”)
fn = input()
print(“Enter your last name:”)
ln = input()
rfname = ln + “ “ + fn
print(“Your full name in reverse order is “, rfname)
print(“Enter a sequence of comma-separated numbers:”)
i = input().replace(“ “, “”).split(“,”)
il = list(i)
it = tuple(i)
print(“List : “, il)
print(“Tuple : “, it)
print(“Input filename:”)
fn = input()
print(fn.split(“.”)[-1])
color_list = [“Red”,”Green”,”White” ,”Black”]print(color_list[0])
print(color_list[-1])
exam_st_date = (11, 12, 2014)print(“The examination will start from : “ + str(exam_st_date[0]) + “ / “ + str(exam_st_date[1]) + “ / “ + str(exam_st_date[2]))
print(“Enter an integer: “)
n = input()
r = int(n) + int(n + n) + int(n + n + n)
print(“Expected Result : “, r)
print(“Enter a built-in function(s)”)
x = input()
help(x)
import calendar
from time import strftime
yy = int(strftime(“%Y”))
mm = int(strftime(“%m”))
print(calendar.month(yy, mm))
print(“””a string that you “don’t” have to escape\nThis\nis a ……. multi-line\nheredoc string — — — → example”””)
from datetime import dated0 = date(2014, 7, 2)
d1 = date(2014, 7, 11)
delta = d1 — d0
print(str(delta.days) + “ days”)
import mathrad = 6
v = (4/3) * math.pi * (math.pow(rad, 3))
print(“Volume: “, v)
print(“Enter a number: “)
n = float(input())
if n > 17:
print(abs(n — 17) * 2)
else:
print(n — 17)
def near_thousand(n):
return (abs(n — 1000) <= 100) or (abs(n — 2000) <= 100)
print(near_thousand(905))
print(near_thousand(1095))
print(near_thousand(1889))
print(near_thousand(878))
print(“Enter first number: “)
n1 = float(input())
print(“Enter second number: “)
n2 = float(input())
print(“Enter third number: “)
n3 = float(input())
if n1 == n2 == n3:
print(3 * (n1 + n2 + n3))
else:
print(n1 + n2 + n3)
def isitthere(s):
w = str.split(s)
if w[0] == “Is”:
return s
else:
return “Is “ + s
print(isitthere(“Time to take a bath!”))
print(isitthere(“Is it time to buy a new bike?”))
print(“Enter a string: “)
s = input()
print(“How many copies of the string do you want?”)
n = int(input())
xs = (s + “\n”) * n
print(xs)
print(“Enter a number: “)
n = float(input())
if n % 2 == 0:
print(“Your number is even.”)
else:
print(“Your number is odd.”)
import numpy as npsample_list = list(np.random.randint(low = 0, high = 10, size = 20))
print(sample_list)
def count_four(list):
x = 0
for i in list:
if i == 4:
x += 1
return x
print(count_four(sample_list))
print(“Enter a string: “)
s = input()
print(“How many copies do we want?”)
n = int(input())
def getcopy(s, n):
x = []
if n >= 2:
x.append(s[0])
x.append(s[1])
print(((x[0] + “ “ + x[1] + “\n”) * (n — 1)) + (x[0] + “ “ + x[1]))
else:
print(s * n)
getcopy(s, n)
print(“Enter a letter: “)
x = input()
def vowelornot(x):
vowel = [“a”, “e”, “i”, “o”, “u”]
if (x in vowel):
print(“Our letter is a vowel.”)
else:
print(“Our letter is not a consonant.”)
vowelornot(x)
test_data = [1, 5, 8, 3]print(“Enter a number: “)
x = int(input())
def check_value(x):
if(x in test_data):
return True
else:
return False
check_value(x)
import matplotlib.pyplot as plt
import numpy as np
sample_list = list(np.random.randint(low = 0, high = 100, size = 10))
print(“Sample list:\n”, sample_list)
plt.hist(sample_list)
plt.show()
from urllib.request import Request, urlopen
import random
url = “https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
req = Request(url, headers={‘User-Agent’: ‘Mozilla/5.0’})
web_byte = urlopen(req).read()webpage = web_byte.decode(‘utf-8’)
words = webpage[:100000].split(“\n”)
random.shuffle(words)
sample_words = words[:10]
print(“Sample elements:\n”, sample_words)
print(“String:”)
‘ ‘.join(sample_words)
numbers = [    
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958,743, 527
]
def no28(nums):
for i in nums:
if i == 237:
break
if i % 2 == 0:
print(i)
no28(numbers)
color_list_1 = set([“White”, “Black”, “Red”])
color_list_2 = set([“Red”, “Green”])
def notin2(list1, list2):
s = set()
for i in list1:
if i not in list2:
s.add(i)
else:
continue
print(s)
notin2(color_list_1, color_list_2)
print(“Input triangle base: “)
b = float(input())
print(“Input triangle height: “)
h = float(input())
a = (h * b) / 2
print(“Triangle area: “, a)
import mathprint(“Input first integer: “)
first = int(input())
print(“Input second integer: “)
second = int(input())
gcd = math.gcd(first, second)
print(“The GCD of the two integers is: “, gcd)
import mathprint(“Input first integer: “)
first = int(input())
print(“Input second integer: “)
second = int(input())
# For Python 3.9.0 and higher
# gcd = math.lcm(first, second)
lcm = int((first * second) / math.gcd(first, second))
print(“The LCM of the two integers is: “, lcm)
print(“Input first integer: “)
first = int(input())
print(“Input second integer: “)
second = int(input())
print(“Input third integer: “)
third = int(input())
if first == second:
sum = 0
elif first == third:
sum = 0
elif second == third:
sum = 0
else:
sum = first + second + third
print(“Output: “, sum)
print(“Input first integer: “)
first = int(input())
print(“Input second integer: “)
second = int(input())
if first + second in range(15, 20):
sum = 20
else:
sum = first + second
print(“Output: “, sum)
print(“Input first integer: “)
first = int(input())
print(“Input second integer: “)
second = int(input())
def tru(first, second):
if first == second or first + second == 5 or abs(first — second) == 5:
return True
tru(first, second)
int_a = 7
int_b = 43
not_int = “o”
def bothint(a, b):
if isinstance(a, int) and isinstance(b, int):
print(a + b)
bothint(int_a, int_b)
print(“Input your name: “)
name = input()
print(“Input your age: “)
age = input()
print(“Input your address: “)
address = input()
print(“Output:\n” + name + “\n” + age + “\n” + address)
x = 4
y = 3
def solver(x, y):
o = (x + y) ** 2
return “(“ + str(x) + “ + “ + str(y) + “) ^ 2 = “ + str(o)
solver(x, y)
amt = 10000
interest = 3.5
years = 7
# compound interest
# Principal × (1 + Rate)Time − Principal
compound = amt + (amt * ((1 + interest / 100) ** years) — amt)
print(round(compound, 2))
import mathprint(“Enter x1: “)
x1 = int(input())
print(“Enter y1: “)
y1 = int(input())
print(“Enter x2: “)
x2 = int(input())
print(“Enter y2: “)
y2 = int(input())
# d=√((x_2-x_1)²+(y_2-y_1)²
d = math.sqrt(math.pow((x2 — x1), 2) + math.pow((y2 — y1), 2))
print(d)
from os.path import exists as file_existsfile_exists(‘/content/sample_data/anscombe.json’)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store