Module 2: Python Fundamentals
This screenshot is of the results from the script made from this lab. It shows the printing of my last name from a list, the dice game results created from my for loop and if/else statements, the number results from my while loop selecting random numbers between 1 and 10, twenty times, and the updated list after using the remove method.
This week's lab was all about exploring key Python programming concepts, including loops, functions, and string handling. We focused on understanding how different loop structure's function, how built-in Python methods operate, and how to properly handle strings using indexing and escape characters.
One of the main takeaways was distinguishing between for loops and while loops. A for loop is used when iterating over a known sequence, such as a list of names. It ensures each item in the sequence is processed exactly once. A while loop, on the other hand, continues running as long as a given condition remains true, making it useful for cases where the number of repetitions is unpredictable.
Another major concept was learning how Python functions are structured. Built-in functions like len(), type(), and range() take an object as input and return useful information. For example, len("hello world") returns the number of characters in the string. Similarly, methods like split() and append() modify objects. Using split(" ") on a string breaks it into a list, which makes it easier to work with individual words.
String handling and indexing were crucial topics as well. Python uses zero-based indexing, meaning the first item is at position 0, and the last item is accessed using -1. This applies to both lists and strings. Additionally, proper string formatting is important. One mistake was using an apostrophe within single quotes, like print('let's go!'), which causes a syntax error. To fix this, either double quotes (print("let's go!")) or escape characters (print('let\'s go!')) must be used.
Some notes from this lab include:
- Remembering that Python is case-sensitive, which means
"X"and"x"are treated differently. - Using
str()to convert an integer to a string when needed. - Saving scripts frequently to avoid losing progress from potential crashes.
- Importing modules like
randomwhen generating random values. - Understanding that
!=means "not equal." - Using methods like
split()to break strings into lists.
Overall, this was a great learning experience in Python programming. These foundational concepts will be useful as I continue building scripts and developing more efficient code.
.png)

Comments
Post a Comment