PYTHON AND JQUERY CODING EXERCISES
CODING FOR BEGINNERS
JJ TAM
PYTHON CODING EXERCISES
CODING FOR BEGINNERS
JJ TAM
Python Basic Exercises
Get Python version
CODE
import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)
OUTPUT
Python version
3.6.6 (default, Jun 28 2018, 04:42:43)
[GCC 5.4.0 20160609]
Version info.
sys.version_info(major=3, minor=6, micro=6, releaselevel='final', serial=0)
Display current date and time
PYTHON CODE
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
OUTPUT
Current date and time :
2020-10-22 10:35:31
Print the calendar
PYTHON CODE
import calendar
y = int(input("Input the year : "))
m = int(input("Input the month : "))
print(calendar.month(y, m))
OUTPUT
Input the year : 2020
Input the month :
October 2020
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Computes the value of n+nn+nnn
PYTHON CODE
a = int(input("Input an integer : "))
n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
print (n1+n2+n3)
OUTPUT
Input an integer :
Calculate number of days
PROGRAM
from datetime import date
f_date = date(2014, 7, 2)
l_date = date(2015, 7, 11)
delta = l_date - f_date
print(delta.days)
OUTPUT
volume of a sphere in Python
PROGRAM
pi = 3.1415926535897931
r= 6.0
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)
OUTPUT
The volume of the sphere is: 904.7786842338603
Compute the area of Triangle
PROGRAM
b = int(input("Input the base : "))
h = int(input("Input the height : "))
area = b*h/2
print("area = ", area)
OUTPUT
Input the base : 20
Input the height : 40
area = 400.0
Compute the GCD
PROGRAM
def gcd(x, y):
gcd = 1
if x % y == 0:
return y
for k in range(int(y / 2), 0, -1):
if x % k == 0 and y % k == 0:
gcd = k
break
return gcd
print(gcd(12, 17))
print(gcd(4, 6))
OUTPUT
CALCULATE THE LCM
PROGRAM
def lcm(x, y):
if x > y:
z = x
else:
z = y
while(True):
if((z % x == 0) and (z % y == 0)):
lcm = z
break
z += 1
return lcm
print(lcm(4, 6))
print(lcm(15, 17))
OUTPUT
Convert feet and inches to centimeters
PROGRAM
print("Input your height: ")
h_ft = int(input("Feet: "))
h_inch = int(input("Inches: "))
h_inch += h_ft * 12
h_cm = round(h_inch * 2.54, 1)
print("Your height is : %d cm." % h_cm)
OUTPUT
Input your height:
Feet: 5
Inches: 3
Your height is : 160 cm.
Convert time seconds
PYTHON CODE
days = int(input("Input days: ")) * 3600 * 24
hours = int(input("Input hours: ")) * 3600
minutes = int(input("Input minutes: ")) * 60
seconds = int(input("Input seconds: "))
time = days + hours + minutes + seconds
print("The amounts of seconds", time)
Output:
Input days: 4
Input hours: 5
Input minutes: 20
Input seconds: 10
The amounts of seconds 364810
Convert seconds to day
PROGRAM
time = float(input("Input time in seconds: "))
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
print("d:h:m:s-> %d:%d:%d:%d" % (day, hour, minutes, seconds))
OUTPUT
Input time in seconds: 1234565
d:h:m:s-> 14:6:56:5
Calculate BMS
PROGRAM
height = float(input("Input your height in meters: "))
weight = float(input("Input your weight in kilogram: "))
print("Your body mass index is: ", round(weight / (height * height), 2))
OUTPUT
Input your height in meters: 6.2
Input your weight in kilogram: 72
Your body mass index is: 1.87
Sort three integers
PROGRAM
x = int(input("Input first number: "))
y = int(input("Input second number: "))
z = int(input("Input third number: "))
a1 = min(x, y, z)
a3 = max(x, y, z)
a2 = (x + y + z) - a1 - a3
print("Numbers in sorted order: ", a1, a2, a3)
OUTPUT
Input first number: 2
Input second number: 4
Input third number: 5
Numbers in sorted order: 2 4 5
Get system time
PROGRAM
import time
print()
print(time.ctime())
print()
OUTPUT
Thu Oct 22 14:59:27 2020
Check a number
PYTHON CODE
num = float(input("Input a number: "))
if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")
OUTPUT
Input a number: 200
It is positive number
Python code to Remove first item
PROGRAM
color = ["Red", "Black", "Green", "White", "Orange"]
print("\nOriginal Color: ",color)
del color[0]
print("After removing the first color: ",color)
print()
OUTPUT
Original Color: ['Red', 'Black', 'Green', 'White', 'Orange']
After removing the first color: ['Black', 'Green', 'White', 'Orange']
Filter positive numbers
PYTHON CODE
nums = [34, 1, 0, -23]
print("Original numbers in the list: ",nums)
new_nums = list(filter(lambda x: x >0, nums))
print("Positive numbers in the list: ",new_nums)
OUTPUT
Original numbers in the list: [34, 1, 0, -23]
Positive numbers in the list: [34, 1]
Count the number 4
in a given list
SAMPLE PROGRAM
def list_count_4(nums):
count = 0
for num in nums:
if num == 4:
count = count + 1
return count
print(list_count_4([1, 4, 6, 7, 4]))
print(list_count_4([1, 4, 6, 4, 7, 4]))
OUTPUT
Find a number even or odd
SAMPLE PROGRAM
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")
OUTPUT
Enter a number: 5
This is an odd number.
Get n copies of a given string
PROGRAM
def larger_string(str, n):
result = ""
for i in range(n):
result = result + str
return result
print(larger_string('abc', 2))
print(larger_string('.py', 3))
OUTPUT
abcabc
.py.py.py
Print out a list which are not present in other list