2026-08-25 21:52
Status:
Tags: IBM IBM Full Stack Software Developer Professional Certificate IBM Python for Data Science, AI & Development Programming Programming Concepts Programming Lanuages Python
PDSAD - Module 3 - Python Programming Fundamentals
==Conditions and Branching
-
Conditions and branching:
- These are pretty cool from a programming perspective, basically, you have this bytecode array, as python would, and then you would basically have this very long array of instructions for your interpreter to execute
- The way you have things like conditionals and branching statements is by basically have a bytecode instructure that would check if the inner condition is true, and if so, it would JUMP that many spaces over in the bytecode array
- Of course the issue is that you need to tell it how many spaces to skip over, but as you are compiling, you have no idea how long it really is, and so you need to keep it empty, compile the inner branch code, and then update that value with the amount you wish to jump
- Anyways, as for python itself, basically it has a
if/elif/elseprogramming structure which makes it pretty unique for how it deals with branches, theelifstatement is under the originalifstatement and basically counts as another branch which is different from most languages which just useelse ifas a switch off between the blocks, but I guess python is special - Obviously, the way a branch works is it has a given conditional inside of it, which if true, runs the code for that branch, if not skips over the code and lets you move it out forward more
- There is also things like the
==, !=, NOToperators which basically let you invert things so if you have a true statement and putNOT truestatmentit now is actually valued over to false
==Loops
-
Loops:
- These are also pretty cool from a programming standpoint because they do work in essence the exact same way that something akin to an if statement would in which you compile the code and fill in the space to skip over
- Except, these are a bit different in the sense that you need to check if a condition is true multiple times and then basically at the end of the compiled block you need to have a bytecode instruction that would tell the interpreter to move BACK to the start of the code block if the condition has not been met
- There are generally two types of loops
forloops & whileloops, in reality it is all just while loops under the hood as every single for loop can just be written as a while loop, and there is also things likeforeachloops which is kind of how python works pretty much - These however, work by instantiating a variable in a given scope for the loop, at which point this variable is checked inside of some user defined condition, and then is incremented or changed using some kind of expression, and then when that condition is FALSE, you no longer tell the pointer of where you are in the array to move backwards
- For while loops this is essentially the same, except its a bit more simple because you just check the condition only, you are not actually holding concept over the variable that is changing for that condition to be true, unless the user writes their code in such a structure
- In python there is generally a
for x in range()syntax which is used in order to create a list of what I can assume is the range you want, so you can increment over that list and run the code that many times over - the thing is that python does also have a case of something like
for char in stringwhich lets you basically get each character in that string, with that word that you got inside of there thatcharbeing whatever name you want it to be - There is also a utility called
for index, fruit in enumerate(fruits)which lets you keep track of multiple different items and their index inside of a list - While loops basically the exact same way that they do in almost every other language, just a good old
while()and then whatever condition you want to check and run
==Functions
-
Functions:
- These are REALLY complicated inside of the virtual machine in a language, basically, whenever they are originally implemented, they nearly touch every single portion of your code base
- They essentially turn your single chunk which holds the entire bytecode vm’s array of instructions to run, each function needs to have it’s own chunk that you need to be able to switch back to and back from every single time that you run one, so you need to basically have two parts, a function declaration and a function call which the function declaration is where you basically compile that function’s own scope and it’s own bytecode array, and then the call which is when you tell the interpreter to actually move over into it
- that and then actually parsing them sucks because of things like the parameters, the function output, and then making sure that they are correctly typed, which python does have to do but my language did, they are generally one of I think the hardest parts to understand inside of a programming language implementation, even if they are the most useful, although closures come in as a close first or second
- Either way, functions inside of python are pretty standard:
def yourFunction(yourArgs): return yourReturnNlogicbasically as it looks there is how it really works, you can pass in data into functions and what not and they work just about as you would expect a function to - There is also the
passkeyword which basically just makes the code go on to the next scope or instruction whenever it ends up hitting onto it, and then inside of python there is also a lot of native functions which my own programming language has as well - You can also have weird scope stuff since python doesn’t have curly brackets which makes functions like the one place scope really matters, so you can access things like global variables inside of there and what not as well
- There is also a thing where I believe you can have things like
keyargsor something where you can have pre-determined values for a given function argument, similar to how you can have in C++ - Also, similar to C++ and many other languages except C, i think python allows you to have a function with the same name but different parameters and what not which is pretty cool
==Exception Handling
-
Exception Handling:
- Basically exceptions are not necessarily programmer bugs or errors, but are instead things that are logically impossible, not just something that you ended up fucking up as a programmer
- A good example of this is if you try to read in a file or something like that, but then the file is actually empty and you are unable to read it, leading you to basically try to access a null point of data resulting in a crash
- That for example would be something like an exception, although generally the idea of exception and error are not too far away from each other in a day to day programming scenario
- There is also then something akin to basically
try catchinside of python, except they do not call it that, it is insteadtry exceptwhere you need to provide the given exception error that it has for you, I do not know if it catches just any error for you or something, it might - But exception handling is basically just that, it is when you generally might not know if some data that you have coming in is valid or not, and so you want to make sure that your program does not crash if the thing you have gets fried
==Objects and Classes
-
Objects and Classes:
- Objects are certainly a bit of a situation as well, they are generally I think maybe a bit less complicated than functions are, but are honestly not too far off, and I do not entirely understand how they might work with something of the things like the in place variables that they have
- Generally, a stateless object or class inside of a programming language, especially an interpreted one is very simple, generally you just have something like a hash-table for the methods where you are able to call each by their name, and then you might have a hash-table for the variables too, which is how javascript lets you just add new shit on the fly btw
- Generally though, if you do not want this behavior, you would need to have the class object variables to just sort of exist on the stack, which is really all handled through the compiler and I do not have the best mental model of such things
- Either way you then also have things like
superwhich is sort of just a variable that is stored inside of a function that is inheriting from another function, and allows you to reference the class methods and variables from the parent class, all cool fine and dandy - And then also
thiswhich basically holds a reference to the class object itself so it can access it’s own variables - Generally Java has WAY more focus on things like objects and classes and the
jloxprogramming language with that weird ass neighbor things and everything is really a bit outside of my current scope of knowledge too, I know how it exists and sort of how to use it but how it works, no idea - Anyways, classes in python are pretty chill but a bit different from something like C++ or Java or even my own language
- You call them the same way using something like
Class yourClassNameand the mainly weird part of it though is if you want to have a constructor because it goes like this
class ClassName: # Class attributes (shared by all instances) class_attribute = value # Constructor method (initialize instance attributes) def __init__(self, attribute1, attribute2, ...): self.attribute1 = attribute1 self.attribute2 = attribute2 # Instance methods (functions) def method1(self, parameter1, parameter2, ...): # Method logic pass def method2(self, parameter1, parameter2, ...): # Method logic pass- Which basically looks like it does, you have really standard stuff but the main weird thing is the constructor and then the
self objectwhich needs to be passed into every single method, most likely so that the actual internal object can store the methods and what not - You create and call different methods the same as you would any other language
objectInstance = object(arg1, arg2)and then you can just call them using the dot operator like soobjectInstance.variable & objectInstance.method(arg1, arg2)and what not, pretty basic stuff - Python objects also have inheritance and what not with also what I can assume to be the
superobject as well, and thats done like so:class child(parent), and it works like how you would expect
==Glossary
| Term | Definition |
|---|---|
| Analogy | Refers to a concept or comparison outside the scope of the programming language itself, used to explain or relate one concept to another in a more understandable way. |
| Attributes | Attributes in Python refer to the characteristics or properties of an object, and they can be accessed using dot notation. |
| Branching | Branching in Python is a process of altering the flow of a program based on conditions, typically using if, elif, and else statements. |
| Comparison operators | Comparison operators in Python are used to compare values and return Boolean results (True or False), including operators like == (equal),!= (not equal), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). |
| Conditions | Conditions in Python are used to make decisions in code, executing specific blocks of code based on whether a given expression evaluates to True or False. |
| Enumerate | In Python, “enumerate” is a built-in function that adds a counter to an iterable, allowing you to loop through both the elements and their corresponding indices. |
| Exception handling | Exception handling in Python is a mechanism for gracefully managing and responding to errors or exceptional conditions that may occur during program execution. |
| Explicitly | In Python, the term “explicitly” refers to performing an action or specifying something in a clear, unambiguous, and direct manner. |
| For loops | For loops in Python are used for iterating over a sequence (such as a list, tuple, or string) or other iterable objects, executing a set of statements for each item in the sequence. |
| Global variable | Global variables in Python are variables defined outside of any function or block and can be accessed and modified from any part of the code. |
| Incremented | ”Incremented” in Python means to increase the value of a variable by a specified amount, typically done using the += operator or by adding a fixed value. |
| Indent | In Python, “indent” refers to the use of whitespace at the beginning of a line to signify the structure and scope of code blocks, such as loops and functions. |
| Indices | In Python, “indices” refer to the position or location of elements in a sequence, like a string, list, or tuple, starting with 0 for the first element. |
| Iterate | In Python, “iterate” means to repeatedly perform a set of operations or steps on each item in a collection, such as a list, tuple, or dictionary, typically using loops or iterators. |
| Local variables | Local variables in Python are variables defined within a specific function or block of code and are only accessible within that function or block. |
| Logic operators | Logic operators in Python are used to perform logical operations on Boolean values, including operators like and (logical AND), or (logical OR), and not (logical NOT). |
| Loops | Loops in Python are constructs for repeating a block of code, enabling the execution of the same code multiple times. |
| Parameters | Parameters in Python are placeholders in a function definition, used to accept and work with values provided to the function when it is called. |
| Programming Fundamentals | Programming fundamentals in Python involve variables, control structures, functions, data structures, input/output, and error handling for building software. |
| Range function | The range function in Python generates a sequence of numbers that can be used for iterating in a loop and is typically used as range (start, stop, step), where it creates numbers from start to stop-1 with the given step increment. |
| Scope of function | The “scope of a function” in Python refers to the region of code where a variable defined within that function is accessible or visible. |
| Sequences | Sequences in Python are ordered collections of items that can include data types like strings, lists, and tuples, allowing for indexing and iteration. |
| Syntax | In Python, “Syntax” refers to the set of rules that dictate how code must be written and structured to be correctly interpreted by the Python interpreter. It includes correct use of keywords, indentation, operators, and punctuation. |
| While loops | While loops in Python are used to repeatedly execute a block of code as long as a specified condition is true. |
==Summary
- Python conditions use “if” statements to execute code based on true/false conditions created by comparisons and Boolean expressions.
- Comparison operations require using comparison operators such as == (equal to), > (greater than), and < (less than).
- Python uses the ”!=” operator to determine whether two values are not equal.
- You can compare integers, strings, and floats.
- Python branching directs program flow by using conditional statements (for example, if, else, elif) to execute different code blocks based on conditions or tests.
- You can use the “if” statement with conditions to define actions if true.
- To perform actions when all previous conditions are false, you can use the “else” statement without a condition.
- The elif statement allows for additional checks only if the initial condition is false.
- To execute various operations on Boolean values, we use Boolean logic operators.
- Python loops are control structures that automate repetitive tasks and iterate over data structures like lists or dictionaries.
- The range() function generates a sequence of numbers with a specified start, stop, and step value for loops in Python.
- A for loop in Python iterates over a sequence, such as a list, tuple, or string, and executes a block of code for each item in the sequence.
- A while loop in Python executes a block of code as long as a specified condition remains true.
- Python functions are reusable code blocks that perform specific tasks, take input parameters, and often return results, enhancing code modularity and reusability.
- You may or may not have written the codes that are often included in functions.
- Python has a set of built-in functions such as “len” to find the length of a sequence or “sum” to find the total sum of a sequence.
- The “sorted” function creates a new sorted list, while “sort” sorts items in the original list.
- You can also create your own functions in Python.
- To ensure clarity and organization and facilitate understanding and maintenance of the code, developers must document functions using a documentation string enclosed in three quotes.
- The help command will return the documentation defined for a particular function.
- A function can have multiple parameters.
- If a function does not include a return statement, it returns None by default.
- You can use the “pass” keyword in a function to indicate that it does nothing (a placeholder for future code).
- A function will usually perform more than one task.
- In Python, the scope of a variable determines where you can access or modify that variable. Global scope allows access from anywhere, while local scope restricts it to a block or function.
- In Python, a programmer defines a local variable within a specific block or function, which can only be accessed or modified within that block or function.
- In Python, a global variable is a variable defined at the top level of a program that any part of the code can access or modify.
- Exception handling in Python is a mechanism for managing and responding to errors and exceptions that may occur during program execution, preventing them from crashing the program.
- In Python, you use the “try-except” statement to attempt a block of code and specify alternative actions to execute if an error occurs, allowing you to handle exceptions.
- In Python, you use the “try-except-else” statement to attempt a block of code, handle exceptions in the “except” block, and execute code in the “else” block when no exceptions occur.
- Python developers use the “try-except-else-finally” statement to attempt a block of code, catch exceptions in the “except” block, execute code in the “else” block when no exceptions occur, and ensure that the “finally” block always runs, regardless of whether exception was raised or not.
- In Python, objects are instances of classes that encapsulate data and behavior, serving as the foundation for creating and working with various data types and custom data structures.
- To determine the type of an object in Python, you can use the
type()command. - Methods may modify an object’s internal state, but the object’s type usually remains the same.
- Classes in Python are blueprints for creating objects, defining their attributes and methods, enabling code organization, and object-oriented programming.
- Function “init” is a special method used to initialize data attributes.
- We can create instances of a class in Python.
- Data attributes consist of the data defining the objects.
- Methods are functions that interact and change the data attributes.
- The method has a function that requires the self as well as other parameters.