Skip to content

Commit b15a2e0

Browse files
authored
Add files via upload
1 parent 5e6f2e2 commit b15a2e0

14 files changed

+333
-0
lines changed

simplePythonScripts/baseVar.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#variables
2+
x = 15 #interger type
3+
4+
y = 5.3 #float type
5+
z = 3.0
6+
7+
first_name = "Miami" #string type
8+
second_name = "miamo yoaleu"
9+
last_name = "HYACINTH"
10+
11+
#converting a float y to interger and storing the values in another variable i
12+
i = int(y)
13+
print(i) #print the value of i and get the different between i and y
14+
15+
#converting an interger x to a float and storing the values in another variable j
16+
j = float(x)
17+
print(j) #print the value of j and get the different between j and x
18+
19+
#multiplication of variable x and y and print the result
20+
print("{0} * {1} = {2}".format(x,y,x*y))
21+
22+
#capitilize the content of the variable second_name
23+
print(second_name.capitalize())
24+
25+
#variable first_name is printed in uppercase letter
26+
print(first_name.upper())
27+
28+
#variable last_name is printed in lowercase letter
29+
print(last_name.lower())
30+
31+
#check if variable first_name is uppercase and answer is in boolean
32+
print(first_name.isupper())
33+
34+
#print a statement with multiple datatype
35+
print("Mr. %s are you %d years old? " %(first_name,x), "\n\tWhy ask?")

simplePythonScripts/conditionif.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
print("Grade List")
2+
print("==========")
3+
4+
#You have to take the marks of the student as input from the user.
5+
# Then assign the grades based on marks obtained by the students.
6+
def gradeAssign(marks):
7+
assert marks >= 0 and marks <= 100
8+
9+
if marks >= 90:
10+
grade = "A"
11+
elif marks >= 70:
12+
grade = "B"
13+
elif marks >= 50:
14+
grade = "C"
15+
elif marks >= 40:
16+
grade = "D"
17+
else:
18+
grade = "F"
19+
20+
return grade
21+
22+
23+
def main():
24+
marks = float(input('Enter your marks: '))
25+
print("Marks: ", marks, "\nGrade: ", gradeAssign(marks))
26+
27+
main()
28+
29+
print("======================================================")
30+
31+
#Python Code To Check If The Year Is A Leap Year
32+
#The year 2000 was a leap year, for example, but the years 1700, 1800, and 1900 were not
33+
def checkYear(year):
34+
if (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0):
35+
print(year, ' is a leap year')
36+
else:
37+
print(year, ' is not a leap year')
38+
39+
40+
def main():
41+
year = int(input('Enter the number of rows: '))
42+
checkYear(year)
43+
44+
45+
main()
46+
47+
print("======================================================")
48+
print("Find the maximum Number")
49+
print("==========")
50+
51+
52+
def FindMaximum(n1, n2, n3):
53+
if n1 > n2:
54+
if n1 > n3:
55+
maxNumber = n1
56+
else:
57+
maxNumber = n3
58+
elif n2 > n3:
59+
maxNumber = n2
60+
else:
61+
maxNumber = n3
62+
return maxNumber
63+
64+
65+
def main():
66+
n1 = int(input("Enter first number: "))
67+
n2 = int(input("Enter Second numer: "))
68+
n3 = int(input("Enter Third number: "))
69+
70+
maximum = FindMaximum(n1, n2, n3)
71+
print("Maximum number is", maximum)

simplePythonScripts/convertToJSON.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
3+
a = 'sring'
4+
b = 25
5+
c = 17.3
6+
d = [2, 3, 5, 6, 7]
7+
e = (7, 3, 5, 6, 2)
8+
9+
#a set cannot be converted into json
10+
f = {2,5, 3, 7, 6}
11+
12+
x = {'name':'Miano', 'age':25, 'gender':'F', 'Nationality':'Cameroon'}
13+
14+
print(json.dumps(x))
15+
print("-------------------------------")
16+
17+
print(json.dumps(e))
18+
print("-------------------------------")
19+
20+
print("indent 3 json fomat",json.dumps(d, indent=3))
21+
print("-------------------------------")
22+
23+
print("indent 2 json fomat",json.dumps(d, indent=2))
24+
print("-------------------------------")
25+
26+
print("indent 1 json fomat",json.dumps(e, indent=1))
27+
print("-------------------------------")
28+
29+
print("indent 0 json fomat",json.dumps(e, indent=0))
30+
print("-------------------------------")
31+
32+
print("separator for dictionary in json fomat",json.dumps(x, separators=("; ","=")))
33+
print("-------------------------------")
34+
35+
#creating a JSON file storing data in
36+
with open('demo.jeson', 'w') as fh:
37+
fh.write(json.dumps(a))
38+
fh.close()
39+
40+
#Opening JSON file in Read mode and read from it
41+
fh =open('demo.jeson', 'r')
42+
print(fh.read())
43+
fh.close()
44+

simplePythonScripts/demo2.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
3+
a = 'sring'
4+
b = 25
5+
c = 17.3
6+
d = [2, 3, 5, 6, 7]
7+
e = (7, 3, 5, 6, 2)
8+
f = {2,5, 3, 7, 6}
9+
x = {'name':'Miano', 'age':25, 'gender':'F', 'Nationality':'Cameroon'}
10+
11+
y = json.dumps(x)
12+
13+
with open('demo.jeson', 'w') as fh:
14+
fh.write(json.dumps(a))
15+
fh.close()
16+
17+
fh =open('demo.jeson', 'r')
18+
print(fh.read())
19+
fh.close()
20+

simplePythonScripts/demoPrime.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import time
2+
#function for timing the execution of another function (function use for decorating other function)
3+
def timer(func):
4+
def wrapper():
5+
before = time.time()
6+
try:
7+
func()
8+
except Exception as e:
9+
print("Invalid input: ", e , "\nEnter a positve whole number:")
10+
print("function took. ", time.time() - before, "second")
11+
12+
return wrapper
13+
14+
#decorator
15+
@timer
16+
#function to check for prime number
17+
def prime():
18+
#take input from user for the checking
19+
num = int(input("Enter number: "))
20+
21+
if num != 1:
22+
23+
for i in range(2,num):
24+
if (num%i ==0):
25+
print("Number is not prime")
26+
break
27+
else:
28+
print("Number is Prime")
29+
30+
31+
else:
32+
print("Number is not prime")
33+
34+
prime()

simplePythonScripts/func.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#user define function called student with parameters
2+
def student(name,age,*marks): #the * on marks makes it a tuple meanin can take multiple elements
3+
print('name:', name)
4+
print('age:', age)
5+
6+
print('chemistry:\t',marks[0], '\nbiology:\t',marks[1],'\nphysic:\t\t', marks[2])
7+
student('Miami', 25, 70, 55, 63)
8+
print("------------------------------")
9+
10+
print(student('mamo',20,58,45,96))
11+
print("------------------------------")
12+
13+
#function with parameter and double ** on sales
14+
def bus_owner(user_name, shop_name, **products):
15+
print("Enter Username: ", user_name)
16+
print("Enter shop_name: ",shop_name)
17+
for keys, values in products.items():
18+
print(keys, ': FCFA',values)
19+
bus_owner('Hyacinth', 'ElectronicShop', API=25100, itel550=55000, televisionV5=101500)

simplePythonScripts/greet.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class ComplexNum:
2+
def __init__(self, r=0, i=0):
3+
self.real = r
4+
self.img = i
5+
6+
def get_data(self):
7+
print(f'{self.real} + {self.img}i')
8+
9+
complex1 = ComplexNum(1,3 )
10+
v = complex1.get_data()
11+
12+
complex2 = ComplexNum(2)
13+
complexAttri =5
14+
print(complex2.real, complex2.img, complexAttri)

simplePythonScripts/if_statement.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#User enter an interger value
2+
num= int(input("Enter number : \n"))
3+
#condition check if number enterred id zero or not
4+
if num != 0:
5+
if num>0: #condition check if number is greater than zero
6+
if num %2 ==0: #condition check if number is greater is divisible by two or not
7+
print("Number is positive even ")
8+
else:
9+
print("Number is a positive odd ")
10+
else:
11+
if num%2 == 0:
12+
print("Number is a Negative even")
13+
else:
14+
print("Number is a Negative odd")
15+
16+
else:
17+
print("number is Zero")
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def prime():
2+
startNum = int(input("Enter Lowest Number: "))
3+
endNum = int(input("Enter Highest Number: "))
4+
for num in range(startNum, endNum + 1):
5+
if num>1:
6+
for i in range(2, num):
7+
if (num%i == 0):
8+
break
9+
else:
10+
print(num)
11+
prime()

simplePythonScripts/iterator.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ListIterator:
2+
def __init__(self, a):
3+
self.__list = a
4+
self.__index = -1
5+
6+
def __iter__(self):
7+
return self
8+
9+
def __next__(self):
10+
self.__index +=1
11+
return self.__list[self.__index]
12+
13+
a = [2, 3, 6, 8, 5, 4]
14+
mylist = ListIterator(a)
15+
it = iter(mylist)
16+
print(it)
17+
print(next(it))

simplePythonScripts/lambda.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def double(x):
2+
return x*2
3+
4+
def add(x , y):pass
5+
#return x + y
6+
7+
#lambda will perform this same operation (function) in line 16 and will override this function at line8 when the function is call
8+
def product(x, y, z):
9+
return x * y * z
10+
11+
#These statments call are functions on their own, they are lambda function which are similar to user define function shown above
12+
doubl = lambda i:i *2
13+
14+
add = lambda x, y : x + y
15+
16+
product = lambda x, y, z: x+y+z
17+
18+
19+
print(doubl(10))
20+
print(add(13, 21))
21+
print(product(10, 11, 13))

simplePythonScripts/list.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# list : a single variable that can take many values called elements
2+
x= [2, 5, 8, 6, 0] #list intergers
3+
y = ['Max', 5, 3.2, [2, 4]] ##it is possible to save multiple data types values in a list
4+
z = ['Max', 'Jhon', 'peter', 1]
5+
6+
print(y[0]) #print out the value of the first element at first index from the list y
7+
8+
print(y[3])
9+
10+
print(z[2])
11+
12+
#insert and remove from a list
13+
print(x.insert(2, 'Tome')) #at position two in the list x, Tom is inserted
14+
print(z.remove('Jhon')) #Jhon is to be remove from the list z
15+
16+
w = z.copy() #this copy the content of z into the list of w
17+
18+

simplePythonScripts/script0.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
x = float(input("Enter first Number: "))
2+
y = float(input("Enter second Number: "))
3+
z = float(input("Enter third Number: "))
4+
print("The Max Value between {0}, {1} and {2} is {3} ".format(x,y,z,max(x,y,z)))

simplePythonScripts/tuple.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#tuple are just ad list but differ from list in that, tuples are immutable(cannot be change once created)
2+
x= (1, 5, 3, 7, 5)
3+
y= ('Man', 5, 8, 'Hyacinth') #it is possible to save multiple data types values in a tuple as list
4+
#you cannot insert or remove anything from the tuple
5+
6+
print(x.count(5)) # count the number of 5 in the tuple
7+
print(len(y)) # display the number if element found in the tuple
8+
del(y) # delete the tuple y

0 commit comments

Comments
 (0)