DataraFlow Week 1: Python Crash Course
The DataraFlow internship kicked off with an introduction to the basics of python programming. Due to my previous experience with programming, it did not take me too long to find my feet. Key data structures like lists, tuples, sets, and dictionaries were explored, and the several data types that occupy them such as strings, integers, floats.
The course also served as a refresher for logic operators such as “and” and “or” and how they affect the result of code based on the boolean values that are outputed. Comparison operators such as “>, >=, <, <=, == and !=” were explored alongside Booleans, map and filter functions, and conditonal if, elif and else statements. Using these concepts, I created functions to serve different purposes as a test of my mastery of the programming language on a basic level.
- I created a simple calculator app that performs addition, subtraction, multiplication, and division.
print("Select an operation to carry out: ")
print("1. ADD")
print("2. SUBTRACT")
print("3. MULTIPLY")
print("4. DIVIDE")
operation = input()
if operation == "1":
num1 = input("Enter first number ")
num2 = input("Enter second number ")
print("The sum is " + str(int(num1) + int(num2)))
elif operation == "2":
num1 = input("Enter first number ")
num2 = input("Enter second number ")
print("The difference is " + str(int(num1) - int(num2)))
elif operation == "3":
num1 = input("Enter first number ")
num2 = input("Enter second number ")
print("The product is " + str(int(num1) * int(num2)))
elif operation == "4":
num1 = input("Enter first number ")
num2 = input("Enter second number ")
print("The result is " + str(int(num1) / int(num2)))
else:
print("Invalid entry")
- I also created a shopping cart that takes in items and their prices, before displaying the total cost.
shoppingCart = {}
while True:
itemName = input('What is the item name?: ')
itemPrice = input('What is the item price?: ')
shoppingCart.update({itemName : int(itemPrice)})
op = input('do you want to add more items? yes/no: ').strip(). lower()
if op == 'no':
break
elif op == 'yes':
continue
else:
print('invalid input')
continue
totalPrice = 0
for item, price in shoppingCart.items():
totalPrice += price
print(totalPrice)
- I created a Guessing Game where players are expected to guess a randomly generated number between 1-20, with hints to guide their next guess.
import random
random_int = random.randint(1, 20)
while True:
guess = input('Try to guess the number between 1 to 20: ')
if int(guess) == random_int:
print ('You have guessed the correct number')
break
elif int(guess) > random_int:
print("Your guess is too high")
elif int(guess) < random_int:
print ("Your guess is too low")
elif int(guess) < 1 and guess > 20:
print("You have guessed outside of the range")
I also wrote a program that saves student name to a file before displaying them, hence improving my python file handling skills.
from pathlib import Path FILENAME = Path("students.txt") def collect_names(): print("Enter student names (blank line to finish):") names = [] while True: name = input("Name: ").strip() if not name: break names.append(name) return names def save_names(names, filename=FILENAME): with open(filename, "w", encoding="utf-8", newline="\n") as f: for n in names: f.write(n + "\n") def load_names(filename=FILENAME): with open(filename, "r", encoding="utf-8") as f: return [line.rstrip("\n") for line in f] def nameDisplay(): names = collect_names() if not names: print("No names entered. Exiting.") return save_names(names) print(f"\nSaved {len(names)} name(s) to {FILENAME}.") print("\nReading back from file...") loaded = load_names() for i, n in enumerate(loaded, start=1): print(f"{i}. {n}") if __name__ == "__main__": nameDisplay()
These are just a few of the python programs I wrote during the first week that put my skills to the test.
I faced some indentation challenges, which made my code block fail sometimes, but all in all it was a good brain racking and critical thinking experience. I hope to learn more from this program going forward as I sharpen my skills on the way to the top.