Python notes - 5/16/24

 Ok, I have begun to learn Python several times. I started a Udemy course and got a few hours in, I took a short, extra-curricular class at WVUP, I watched many of Paul McWhorter’s videos on YT, I had to do some Python in the Discrete Math class at WVUP, and all that was on top of Intro to Programming at WVUP, which was Java. So this summer (2024), I am starting a Udemy course called “Automate the Boring Stuff.” I’ve heard good things about it, and while I hate not finishing the other Udemy course I started, I’m going to go ahead with this one. Automating stuff is definitely the kind of thing I want to learn to do with Python, since I am studying Network Engineering, so this course appeals to me. 


https://automatetheboringstuff.com/


I’m starting at the beginning of the course, even though I know this stuff, but it is a good refresher.


The first video was your traditional “Hello World” program and info about ints, strings, expressions, concatenations, and basic stuff like that. I do know these things, but it is good to hear again. I feel like I have such a mental block when it comes to coding, but I also know it is a steep learning curve and I have overcome steep learning curves before. 🙂


The next video is about boolean, and comparison operators. There are 6 comparison operators and they evaluate to a boolean value.

 

The 3 boolean operators are ‘and’, ‘or’, and ‘not’.

 The expression ‘not True’ evaluates to False, and the expression ‘not False’, evaluates to True. 



Flow Control Statements


In this lesson, he had the student write this block of code - 

I explained blocks and I don’t think I’ve ever heard it explained so well. A block starts after each colon. The colon tells you that a new block is beginning. You can also see the indents. 


With this block of code, we see that when the condition is true, the block runs, when it is false, it exits out and prints ‘Done’. 


Next he explains the else statement and we write this program:


If the statement is true, the first block is executed. If the statement is false, the else block is executed. Only one block will be executed. 


Next we wrote this code -

We see that it runs through the elif statements until it finds a true one and the it is finished. The order of the elif statements does matter.  If we were to add an else statement, it would only run if all of the elif statements had been false. An else statement comes at the end.

“Truthy” and “Falsey” statements.

Next we wrote this code - 

If anything at all is entered in the input function, it is considered ‘truthy’, if it is left blank, it is ‘falsey’.  If something is entered, the if block runs, if nothing is entered, the else block runs. 

For integers, the 0 value is falsey and everything else is truthy. For floats, the 0.0 value is falsey, and everything else is truthy. 


While Loops

This while loop iterates 5 times, the variable spam is increased by 1 each time and the output is - 

while statements jump back to the beginning to re-check the condition. A break statement causes the execution to immediately leave the loop, without re-checking the condition. A continue statement, causes the execution to immediately jump back to the start of the loop and re-check the condition. 










For Loop

A for loop iterates a specific number of times.

We wrote this code. The first time the for loop run, the i is set to 0, then it runs again and i is set to 1. This keeps happening until it runs 5 times. 


The next lesson talked about importing modules and he had the students import pyperclip using the CLI. Pyperclip allows you to copy and paste text. 


Functions

I have had so much trouble understanding functions, but here we go. A function is a program that is run when called. Python contains many built-in functions, but we can write our own. 


Argument: The value passed in the function call.

Parameter: The variable inside the function.





Name is the parameter for the function hello. When the hello function is called the first time, ‘Alice’ is passed as the argument to the name parameter. 


Local vs Global scopes

Local variables exist inside of functions. They are destroyed once the function is done. They do not exist nor can be called outside of the function. 


05/10/24

Try and except statements.

This can be used as an input validation method. When you know an error will happen, you can use the try and except method to display a message so that the program can keep running. 


Next lesson:


We wrote a program. I am going to explain every part of this to help me get it and remember. 

The import random line is importing the random function so that we can get a random number for this game.

Then we print the ‘Hello…” line.

The user inputs their name and it is held in the name variable.

Then we print the next line where we state that the program is thinking of a number.

The program then randomly generates the number and stores it in the secretNumber var.


The for loop. This is where I don’t know how to explain it because I barely understand it. For guessesTaken in range(1, 7): guessesTaken is now a variable that the range function is running through. I do understand what the range function is doing, so I will not explain it. 

We print, Take a guess. Then the user inputs a guess, the int function converts it to an int, if it is not, and it is stored in the variable guess. Then we are still in the for loop. If guess is less than secretNumber, it prints ‘your guess is too low’ and then the other way around for the elif statement. The else statement is for the correct guess. 


The if statement is when the guess is equal to the secretNumber, then we print that statement.

The else statement is for when there have been too many guesses, it goes ahead and gives the number.


Lists


Lists are held in brackets. Lists can contain other lists. These would be held in secondary brackets.




For Loops

A for loops repeat the code once for each value in a list or list-like value.


For i in range(len(someList))

This is a common method in python


Variables can swap their values using multiple assignemnet. 

Augmented assignment operators like += are used as shortcuts. 


Methods

Methods are the same things a functions except called upon a certain value.

Append can only be added to lists, not strings.

Same with remove.


We learned about immutable values, vs. mutable values. I think I understand it already, but he recommended a yt video - Facts and myths about python names and values by Ned Batchelder. Ill look it up later and watch it. 



Dictionary data type

Dictionaries are like lists, but can hold many different data types, not just integers.

There are key/value pairs. Typed with curly braces. The key is first, then a colon, then the value. You can access the value through the key. Items in dictionaries are unordered, which is not like lists.

You can check if a key exists in a dictionary  with the in or not operator.

There are 3 methods for returning items in the dictionary. .values(), .keys(), and .items().


To prevent an error message like this -

Use an if statement. 

But this would be tedious, so we use a get() method.

The second ^ argument tells the program what to return if there is no key.

The setdefault() method is a shortcut to ensure that the key exists and there is a default value.


We wrote this program - 

The for loop iterates through every character in the string. The count is a dictionary which we set the default to 0, so it would start at 0. 

Then we added the pretty print module - pprint, by importing it, then adding the last line - 

with this output-

 


Comments

Popular posts from this blog

Project Management

Welcome to my portfolio!