Skip to content

Commit b0ac911

Browse files
author
Ebad8931
committed
Python Workshop Lesson 4 added.
1 parent e4ec6e7 commit b0ac911

4 files changed

+114
-0
lines changed

04_average_of_10_random_numbers.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from statistics import mean as avg
2+
import random as r
3+
4+
numbers = []
5+
for _ in range(10):
6+
numbers.append(r.randint(1, 50))
7+
8+
print(numbers)
9+
print(avg(numbers))

04_functions.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from random import randint
2+
3+
# Previous Examples of functions
4+
print('Hello world')
5+
range(5, 17, 2)
6+
len('python')
7+
8+
9+
# defining a function
10+
def hello():
11+
print('hello world')
12+
13+
14+
# calling the function
15+
hello()
16+
17+
18+
def add(num1, num2): # num1 and num 2 are arguments
19+
return num1 + num2 # result is returned
20+
21+
22+
# Function Calling
23+
total = add(7, 9)
24+
print(total)
25+
26+
27+
def add_numbers(*args):
28+
total = 0
29+
for num in args:
30+
total += num
31+
return total
32+
33+
34+
print(add_numbers(5, 6, 8, 4, 2))
35+
print(add_numbers(234, 543))
36+
print(add_numbers(34, 56, 78, 5, 5.67, 2.45, 5*4))
37+
38+
39+
def cool_function(a, b=4, c=10):
40+
# b and c are default value arguments
41+
print('a is', a, 'b is', b, 'c is', c)
42+
43+
44+
cool_function(38, 49) # a&b are passed c takes its default value
45+
cool_function(54, c=74) # c is keyword argument, b is default argument
46+
cool_function(c=72, a=23) # a&c are keyword arguments, b is default argument
47+
48+
data = [45, 35, 67] # a = data[0], b = data[1], c = data[2]
49+
cool_function(*data) # * unpacks the list and passes it to the function
50+
51+
# random.randint(a,b) generates a random number N such that a <= N <= b
52+
print(randint(1, 10))
53+
print(randint(1, 10))

04_time_conversion.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Getting input
2+
time12 = input("Enter time in hh:mm:ssAM or hh:mm:ssPM ")
3+
# time12 and time24 have common minutes and seconds [:mm:ss]
4+
time24 = time12[2:8]
5+
# str12 format: hh:mm:ssAM or hh:mm:ssPM
6+
am_or_pm = time12[8]
7+
# Slicing hours
8+
hour = time12[0:2]
9+
10+
if am_or_pm == 'A':
11+
# for str12 01:00AM to 11:59AM
12+
if hour != '12':
13+
time24 = hour + time24
14+
# for str12 12:00AM to 12:59AM
15+
else:
16+
time24 = "00" + time24
17+
else:
18+
# for str12 01:00PM to 11:59PM
19+
if hour != '12':
20+
hr = int(hour) + 12
21+
time24 = str(hr) + time24
22+
# for str12 12:00PM to 12:59PM
23+
else:
24+
time24 = hour + time24
25+
26+
print('Time in 24-hour clock format:', time24)

04_time_conversion_function.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Problem Link - https://www.hackerrank.com/challenges/time-conversion/problem
2+
3+
4+
def time_conversion(time12):
5+
time24 = time12[2:8]
6+
am_or_pm = time12[8]
7+
hour = time12[0:2]
8+
9+
if am_or_pm == 'A':
10+
if hour != '12':
11+
time24 = hour + time24
12+
else:
13+
time24 = "00" + time24
14+
else:
15+
if hour != '12':
16+
hr = int(hour) + 12
17+
time24 = str(hr) + time24
18+
else:
19+
time24 = hour + time24
20+
return time24
21+
22+
23+
print(time_conversion('07:05:45AM'))
24+
print(time_conversion('12:05:45AM'))
25+
print(time_conversion('07:05:45PM'))
26+
print(time_conversion('12:05:45PM'))

0 commit comments

Comments
 (0)