Skip to content

Commit 97d65d6

Browse files
committed
Initial commit
0 parents  commit 97d65d6

9 files changed

+343
-0
lines changed

__pycache__/utility.cpython-310.pyc

424 Bytes
Binary file not shown.

controlflowstatements.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
# Conditional Statements
5+
# check for age category
6+
age = 18
7+
if age < 18:
8+
print("You are a minor")
9+
elif age == 18:
10+
print("You just became an adult")
11+
else:
12+
print("You are an adult")
13+
14+
# For Loops
15+
# Loop through a range of numbers
16+
for i in range(1,6):
17+
print(f"Number: {i}")
18+
19+
fruits = ["Aples", "orange", "tomato", "mango"]
20+
for fruit in fruits:
21+
print(f"I like {fruit}.")
22+
23+
# While Loops
24+
# Countdown example
25+
count = 5
26+
while count > 0:
27+
print(f"Countdown: {count}")
28+
count -= 1
29+
print("liftoff!")
30+
31+
# Using break and continue
32+
# Break out of a loop
33+
for num in range(10):
34+
if num == 5:
35+
print("Found 5, stopping loop.")
36+
break
37+
print(f"Number: {num}")
38+
39+
# Skip iteration with continue
40+
for num in range(10):
41+
if num % 2 == 0: # Skip even numbers
42+
continue
43+
print(f"Odd Number: {num}")
44+
45+
# Looping Through Strings
46+
# Print each character of a string
47+
name = "Python"
48+
for char in name:
49+
print(char)
50+

datatypes.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import numpy as np
2+
import pandas as pd
3+
# Declaring Variables
4+
age = 30 # integer
5+
name = "Wanjala" # character
6+
height = 5.9 # float
7+
is_student = True # Boolean
8+
print("Age:", age)
9+
print("Name:", name)
10+
print("Height:", height)
11+
print("Is_student:", is_student)
12+
13+
# Check data types
14+
print("Type of Age:", type(age))
15+
print("Type of Name:", type(name))
16+
print("Type of Height:", type(height))
17+
print("Tpye of Is_student:", type(is_student))
18+
19+
age_str = str(age)
20+
height_int = int(height)
21+
is_student_int = int(is_student)
22+
23+
print("Age as string:", age_str)
24+
print("Height as integer:", height_int)
25+
print("Is_student as integer:", is_student_int)
26+
27+
# String manipulation
28+
29+
# string contenation
30+
full_name = name + "Joel"
31+
print("Full Name:", full_name)
32+
33+
# String formatting
34+
introduction = f"My name is {name}, I am {age} years old."
35+
print(introduction)
36+
37+
# String methods
38+
print("Upper Case Name:", name.upper())
39+
print("Name length:", len(name))
40+
41+
# Arithmetic operations
42+
x = 999
43+
y = 455
44+
45+
print("Addition:", x + y)
46+
print("Subtraction:", x - y)
47+
print("Multiplication:"(), x * y)
48+
print("Division:", x / y)
49+
print("Floor Division:", x + y)
50+
print("Modulus:", x % y)
51+
print("Exponentiation:", x ** y)

file-handling.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Reading a file
2+
import pandas as pd
3+
df = pd.read_csv("OptionPrices.csv")
4+
print(df.head(5))

functions.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import pandas as pd
3+
# 1. Defining and Calling a Function
4+
# Define a function
5+
def greet():
6+
print("Hello, welcome to Python programming!")
7+
8+
# Call the function
9+
greet()
10+
11+
# 2. Function with Parameters and Return Values
12+
# Function to add two numbers
13+
def add_numbers(a, b):
14+
return a + b
15+
16+
# Call the function
17+
result = add_numbers(5, 10)
18+
print("Sum:", result)
19+
20+
## 3. Function with Default Arguments
21+
# Function with a default argument
22+
def introduce(name, age=18):
23+
print(f"My name is {name} and I am {age} years old.")
24+
25+
# Call the function
26+
introduce("Alice")
27+
introduce("Bob", 25)
28+
29+
# 4. Using args for Variable-Length Arguments
30+
# Function to calculate the sum of multiple numbers
31+
def calculate_sum(*args):
32+
return sum(args)
33+
34+
# Call the function
35+
print("Sum:", calculate_sum(1, 2, 3, 4, 5))
36+
37+
## 5. Using kwargs for Keyword Arguments
38+
# Function to print user details
39+
def display_info(**kwargs):
40+
for key, value in kwargs.items():
41+
print(f"{key}: {value}")
42+
43+
# Call the function
44+
display_info(name="Alice", age=30, city="Nairobi")
45+

main.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import numpy as np
2+
import pandas as pd
3+
print("Hello, world!")
4+
5+
# Data types and fariables
6+
age = 30 # integer
7+
print("Age:", age)
8+
name = "Wanjala" # character
9+
print("Name:", name)
10+
height = 5.9 # float
11+
print("Height:", height)
12+
is_student = True
13+
print("Is_Student:", is_student)
14+
15+
age_str = str(age)
16+
print("Age as string:", age_str)
17+
height_int = int(height)
18+
print("Height as string:", height_int)
19+
is_student_int = int(is_student)
20+
print("Is_student as string:", is_student_int)
21+
22+
full_name = name + "Joel"
23+
print("Full Name:", full_name)
24+
25+
introduction = f"My name is {name}, I am {age} years old."
26+
print(introduction)
27+
28+
print("Upper case:", name.upper())
29+
print("Length of Name:", len(name))
30+
31+
# Arithmetic operations
32+
x = 10
33+
y = 3
34+
35+
print("Addition:", x + y)
36+
print("Subtraction:", x - y)
37+
print("Multiplication:", x * y)
38+
print("Difision:", x / y)
39+
print("Floor difision:", x + y)
40+
print("Modulus:", x % y)
41+
print("Exponentiation:", x ** y)
42+
43+
if age < 30:
44+
print("You are now a minor")
45+
elif age == 30:
46+
print("You are now an adult")
47+
else:
48+
print("You are an adult")
49+
50+
for i in range(1,6):
51+
print(f"Number: {i}")
52+
53+
fruits = ["apple", "banana", "mango", "orange"]
54+
for fruit in fruits:
55+
print(f"I like {fruit}")
56+
57+
count = 5
58+
while count > 5:
59+
print(f"countdown: {count}")
60+
count -= 1
61+
print("liftoff!")
62+
63+
for num in range(10):
64+
if num == 5:
65+
print("Found 5: Stop looping")
66+
break
67+
print(f"Number: {num}")
68+
69+
for num in range(10):
70+
if num % 2 == 0:
71+
continue
72+
print(f"Odd Numbers: {num}")
73+
74+
for char in name:
75+
print(char)
76+
77+
def greeting():
78+
print("Hello, world")
79+
greeting()
80+
81+
def add_numbers(a,b):
82+
return a + b
83+
result = add_numbers(589738986282625,7899425288736)
84+
print("sum:", result)
85+
86+
def introduce(name, age = 30):
87+
print(f"My name is {name} and I am {age} years old.")
88+
89+
introduce("Wanjala")
90+
introduce("Bob", 25)

modules-libraries.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Using math library
2+
import math
3+
print("Square root of 999:", math.sqrt(999))
4+
print("Falue of:", math.pi)
5+
print("Sine of 269:", math.sin(math.radians(269)))
6+
7+
# Using the datetime library
8+
from datetime import datetime
9+
now = datetime.now()
10+
print("Current Date and Time:", now)
11+
12+
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
13+
print("Formatted Date:", formatted_date)
14+
15+
# Using random library
16+
import random
17+
print("Random number between 1 and 10:", random.randint(1,10))
18+
fruits = ["apple", "banana", "cherry"]
19+
print("Random fruit:", random.choice(fruits))
20+
21+
random.shuffle(fruits)
22+
print("Shuffled fruits:", fruits)
23+
24+
# importing a custom module
25+
import utility
26+
print(utility.greet_user("Joel"))
27+
print("Square of 5:", utility.square_number(5))

oop.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Defining a class and creating an object
2+
class Animal:
3+
def __init__(self, name, species):
4+
self.name = name
5+
self.species = species
6+
7+
def describe(self):
8+
print(f"{self.name} is a {self.species}.")
9+
10+
dog = Animal("Buddy", "dog")
11+
dog.describe()
12+
13+
# Instance fs. class attributes
14+
class circle:
15+
pi = 3.14159
16+
def __init__(self, radius):
17+
self.radius =radius
18+
19+
def area(self):
20+
return circle.pi * self.radius ** 2
21+
22+
circle1 = circle(5)
23+
print("Area of circle:", circle1.area())
24+
25+
# Inheritance
26+
class fehicle:
27+
def __init__(self, brand, wheels):
28+
self.brand = brand
29+
self.wheels = wheels
30+
31+
def describe(self):
32+
print(f"This is a {self.brand} fehicle with {self.wheels} wheels.")
33+
34+
class car(fehicle):
35+
def __init__(self, brand, wheels, doors):
36+
super().__init__(brand, wheels)
37+
self.doors = doors
38+
39+
def describe(self):
40+
super().describe()
41+
print(f"It has {self.doors} doors.")
42+
43+
my_car = car("Toyota", 4, 4)
44+
my_car.describe()
45+
46+
# Encapsulation
47+
class bankaccount:
48+
def __init__(self, owner, balance):
49+
self.owner = owner
50+
self.__balance = balance
51+
52+
def deposit(self, amount):
53+
if amount > 0:
54+
self.__balance += amount
55+
print(f"Deposited {amount}. New balance: {self.__balance}")
56+
57+
def withdraw(self, amount):
58+
if amount > 0 and amount <= self.__balance:
59+
self.__balance -= amount
60+
print(f"Withdraw {amount}. New balance: {self.__balance}")
61+
else:
62+
print(f"Insufficient funds or infalid amount.")
63+
64+
def get_balance(self):
65+
return self.__balance
66+
67+
account = bankaccount("Joel", 1000)
68+
account.deposit(500)
69+
account.withdraw(300)
70+
print("Final Balance:", account.get_balance())

utility.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# creating module
2+
def greet_user(name):
3+
return f"Hello, {name}! Welcome to the module example."
4+
5+
def square_number(num):
6+
return num ** 2

0 commit comments

Comments
 (0)