Content-Length: 669054 | pFad | http://github.com/farhanjaved47/python/commit/fda20ebb2d269c7927ab9e6ccf9de50e8951595b

86 Python 3 Math Module · farhanjaved47/python@fda20eb · GitHub
Skip to content

Commit fda20eb

Browse files
committed
Python 3 Math Module
1 parent 98989a9 commit fda20eb

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

Python3StandardLibrary/BuiltIn.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/11/2019
5+
Working with basic Python built in functions
6+
"""
7+
8+
9+
def minimum(a, b):
10+
return min(a, b)
11+
12+
13+
def roundOff(number):
14+
return round(number)
15+
16+
17+
def absoluteValue(number):
18+
return abs(number)
19+
20+
21+
def power(base, exponent):
22+
return pow(base, exponent)
23+
24+
25+
def main():
26+
print(min('Farhan', 'Farah')) # string is compared based on which letter comes first
27+
print(round(41.3))
28+
print(absoluteValue(-2323.2))
29+
probabilityOfOne = 1/6
30+
numberOfTimes = 2
31+
print(power(probabilityOfOne, numberOfTimes))
32+
33+
# Smallest to Largest
34+
pointsInAGame = [0, -10, -15, -2, 1, 12]
35+
sortedGame = sorted(pointsInAGame)
36+
print(sortedGame)
37+
38+
names = ['Sue', 'Jerry', 'Linda']
39+
print(sorted(names))
40+
mixedCase_names = ['Sue', 'Jerry', 'Linda']
41+
print(sorted('My favourite child is Linda'.split(), key=str.upper))
42+
43+
print(sorted(pointsInAGame, reverse=True))
44+
45+
leaderBoard = {
46+
231: 'Farhan',
47+
123: 'Alfred',
48+
432: 'Ryan'
49+
}
50+
51+
print( leaderBoard.get((sorted(leaderBoard, reverse=True))[0]))
52+
# print the value of the highest score in a dictionary
53+
# (Score, Name)
54+
55+
students = [('alice', 'B', 12), ('eliza', 'A', 16), ('tega', 'C', 9), ('harry', 'A+', 18)]
56+
# name, grade, marks
57+
print(sorted(students, reverse=True, key=lambda student: student[2])) # sorting on the marks of each student
58+
59+
60+
if __name__ == '__main__': main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/15/2019
5+
Basic Math functionality in python 3 - IterTools module
6+
https://docs.python.org/3/library/math.html
7+
"""
8+
import itertools
9+
10+
11+
def main():
12+
# Infinite Counting
13+
for x in itertools.count(50, 100): # will count from 50 to infinity
14+
print(x)
15+
if x == 150: # or any other condition
16+
break
17+
counter = 0
18+
# Infinite Cycling
19+
for c in itertools.cycle([1, 'a', 2, 'b']):
20+
print(c)
21+
counter += 1
22+
if counter == 20:
23+
break
24+
# Infinite Repeating
25+
26+
for r in itertools.repeat(True):
27+
print(r)
28+
counter += 1
29+
if counter == 40:
30+
break
31+
# Permutations and Combinations
32+
# Permutations is different orders of things. Order does matter - Factorial
33+
# Combinations takes different combinations of a smaller set from a larger set. Order does not matter.
34+
35+
election = {1: 'Barb', 2: 'Karen', 3: 'Erin'}
36+
for p in itertools.permutations(election.values()):
37+
print(p) # 3! basically
38+
39+
colors_for_painting = ['Red', 'Blue', 'Orange', 'Purple', 'Pink']
40+
for c in itertools.combinations(colors_for_painting, 2):
41+
print(c)
42+
43+
44+
if __name__ == '__main__': main()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/15/2019
5+
Basic Math functionality in python 3 - Part 1
6+
https://docs.python.org/3/library/math.html
7+
"""
8+
import math
9+
10+
11+
def main():
12+
# constants in the math module
13+
print(math.pi) # Pi constant
14+
print(math.e) # The Euler's constant
15+
print(math.nan) # Not a number
16+
print(math.inf) # +ive infinity
17+
print(-math.inf) # -ive infinity
18+
19+
# basic trigonometry
20+
# trigonometric functions take angles in randians
21+
angle_in_degrees = 90
22+
angle_in_radians = math.radians(angle_in_degrees)
23+
print(math.sin(angle_in_radians))
24+
print(math.cos(angle_in_radians))
25+
print(math.tan(angle_in_radians))
26+
27+
# Ceiling and floor functions work on the closest integer
28+
my_age = 24.6
29+
my_salary = 45.8
30+
print(f'My age is {math.floor(my_age)}')
31+
print(f'My salary is {math.ceil(my_salary)}')
32+
33+
34+
if __name__ == '__main__': main()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/15/2019
5+
Basic Math functionality in python 3 - Part 2
6+
https://docs.python.org/3/library/math.html
7+
"""
8+
import math
9+
10+
11+
def main():
12+
factorial = 10
13+
square_root = 289
14+
print(math.factorial(factorial)) # prints the factorial of any given number
15+
print(math.sqrt(square_root)) # prints the square root of the given number
16+
17+
# Math module also provide a built in function to calculate the GCD
18+
# GCD is the greatest common divisor for 2 number. Helps to reduce large fractions
19+
numerator = 2048
20+
denominator = 1024
21+
print(math.gcd(numerator, denominator))
22+
23+
24+
if __name__ == '__main__': main()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/15/2019
5+
Basic Math functionality in python 3 - Random functions
6+
https://docs.python.org/3/library/math.html
7+
"""
8+
import random
9+
10+
11+
def main():
12+
print(random.random()) # prints a random number between 0 and 1
13+
decider = random.randrange(2) # randrange(2) will give a value of 0 or 1 as it is exclusive.
14+
if decider:
15+
print('tails')
16+
elif not decider:
17+
print('heads')
18+
19+
print(random.randrange(1, 7)) # mimic a dice roll
20+
21+
lottery_winners = random.sample(range(100), 5)
22+
print(sorted(lottery_winners))
23+
24+
possible_pets = ['cat', 'dog', 'lizard']
25+
print(random.choice(possible_pets))
26+
27+
cards = ['Jack', 'Queen', 'King', 'Ace']
28+
random.shuffle(cards)
29+
print(cards)
30+
31+
32+
33+
if __name__ == '__main__': main()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/15/2019
5+
Basic Math functionality in python 3 - Statistics module
6+
https://docs.python.org/3/library/math.html
7+
"""
8+
import statistics
9+
10+
11+
def main():
12+
agesData = [10, 13, 14, 12, 11, 10, 11, 10, 15]
13+
print(statistics.mean(agesData))
14+
print(statistics.mode(agesData))
15+
print(statistics.median(agesData))
16+
print(sorted(agesData))
17+
18+
print(statistics.variance(agesData)) # the variance can take an optional second argument of mean (xbar)
19+
print(statistics.stdev(agesData)) # the variance can take an optional second argument of mean (xbar)
20+
21+
22+
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/farhanjaved47/python/commit/fda20ebb2d269c7927ab9e6ccf9de50e8951595b

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy