Posts

Using a Rectangle Class in Python

This week’s assignment was about working with classes and objects in Python. I implemented a Rectangle class using object-oriented programming and tested it with several helper functions. Code Procedure: Created a Point class to represent the (x, y) corner of a rectangle. Defined a Rectangle class that stores position, width, and height. Implemented the following helper functions: create_rectangle(x, y, width, height) - creates a new Rectangle instance. str_rectangle(rect) - returns a string representation of the rectangle. shift_rectangle(rect, dx, dy) - modifies the rectangle’s position in place. offset_rectangle(rect, dx, dy) - returns a new rectangle offset from the original. Tested the functions using the example given in the assignment to ensure they worked as expected. This assignment helped me understand how class instances can be passed into and returned from functions, and how methods like __str__ improve output readability. Code: #...

Python Assignment - Print and String in Python

I ran a Python script using the print ()   function and string variables. Code: # Example 1: Basic print statement print('Hello, world!') # Example 2: Creating a string using single quotes String1 = 'Welcome to the Geeks World' print("String with the use of Single Quotes:") print(String1) Output: Hello, world! String with the use of Single Quotes: Welcome to the Geeks World Explanation: The print () function is used to display output to the screen. In this case, it printed both a simple message ( Hello, world! ) and a string stored in a variable. Strings in Python are sequences of characters enclosed in either single quotes ( ' ) or double quotes ( " ) . You can use either style, but they must match (open and close the same way). In this example: I created a string called String1 using single quotes. Then I used print() to output both a label and the string itself. Together, print() and string variables work perfectly because print() ...

Python Assignment - Roots & Reciprocals

This week I completed an assignment that covered: Solving a quadratic equation using the quadratic formula Printing reciprocals using a for loop Problem 1: Roots of a Quadratic Equation: x² - 5.86x + 8.5408 = 0 Code: import math a = 1 b = -5.86 c = 8.5408 discriminant = b**2 - 4*a*c root1 = (-b + math.sqrt(discriminant)) / (2*a) root2 = (-b - math.sqrt(discriminant)) / (2*a) print("Root 1:", root1) print("Root 2:", root2) Output: Root 1: 3.1400000000000006 Root 2: 2.7199999999999998 Problem 2: Reciprocals (1/2 to 1/10) Code: for i in range(2, 11):     print(f"1/{i} = {1/i}") Output: 1/2 = 0.5 1/3 = 0.3333333333333333 1/4 = 0.25 1/5 = 0.2 1/6 = 0.16666666666666666 1/7 = 0.14285714285714285 1/8 = 0.125 1/9 = 0.1111111111111111 1/10 = 0.1

Python Assignment – Intro to Functions and Lists

  Here’s what I accomplished: Wrote a function that prints a 3-line greeting using someone’s name on each line. Stored three names in a list and used a loop to greet them all. Created a function that prints a nicely formatted full name from first and last name. Built an addition calculator that prints results, then rewrote it to return the result instead. Learned that pow(16, 1/2) works to get the square root of 16 (returns 4.0 ). Played with list and tuple syntax: Found out that 3 * x repeats a list. You can do x + y (joins the lists) but can’t do x - y . Lists are mutable, but tuples can’t be changed. I didn’t run into any issues using the online Python compiler, and I’m already seeing how functions and data structures come together in Python. Looking forward to the next challenge. The Output: Hello, Alice Smith! Hope you're having a great day. Alice Smith, just wanted to say you're awesome! Take care, Alice Smith! --- Hello, Bob Johnson!...

Starting My Python Journey

Hi everyone! This blog will track my progress in LIS-4930: Introduction to Python . I’ll be sharing what I’m learning, cool projects, and tips as I go. Excited to dive into coding!