Photo by Ivan Yeung on Unsplash
Python: I'm still not wise to bit wise operators. Today I Learned: Sunday, June 19 2022
Continuing with Python Institute's CPEC course on Edube.org:
Looking forward to getting this finished so I can take the CPEC exam.
When using the input() function, you can enter an optional string argument into the function. Eg:
name = input("Enter your name: ")
print("Hello, " + name + ". Nice to meet you!")
= and <= are reffered to as the non-strict variants of > and < (greater than and less than)
For indentation, you only use either tabs or spaces. Do not mix and match, Python won't understand it. Choose one or the other and stick with it.
The max() function will return the largest integer passed into it as an argument. Eg.
n = max(2, 6, 17)
print(n)
17
Similarly, you can use the min() funtion to find the smallest number.
while loops only run if the condition equates to true.
- Even if it is the first run of the loop, it will not run if the condition is false.
If you get into an infinite loop on the console or idle, you can press Cntrl-C to break out of the loop.
You can repeat a request for user input by putting an input() function at the end of a while loop.\ You can break out of it by adding some logic. Eg.
number = int(input("Enter a number or type 0 to stop: "))
while number != 0:
- do action
You can print multi-line strings by using triple quotes before and after the string.
'control variable' is the name of the variable that comesafter the keyword 'for' in a for loop.
'syntactic candy' is a phrase, not sure what it means yet.
break and continue:
when break is used in a loop, it immediately breaks out of the loop and runs the next line of code.
when continue is used in a loop, it immediately jumps to the end of the turn, skipping any instructions after it, and starting the loop again. Eg.
for i in range(6):
if i == 3:
- continue
print(I)
This will print: 0, 1, 2, 4, 5.
Notice how the 3 was skipped.
while and for loops also have 'else' keywords. They are always executed once, regardless if the loop has entered the body or now. The else should be at the same indentation level as the while or for statement.
in for loops, the else keyword will not be triggered if the control variable is never created in the body of the loop. Eg.
for i in range(0)
- pass
else:
- print(I)
The above code will not print the else statement because i was never created. (Because the range is 0, there is no iterables to start 'i' with.)
With the print() function, I'm still confused by the end = "" keyword.
Ok, looks like we're back on bit wise operators. Here we go again...
- There wasn't a lot of information in there about bitwise operators. I hope there arent many questions about them on the test.
Back on to lists. I like lists, but I didn't really get to explore multi-dimensional lists in the last course. Hopefully this one will explain them better.
"A list is a collection of elements, but each element is a scalar."
In a list, an element with an index equal to -1 is the last one on the list, and -2 is the 2nd last.
numbers = [111, 7, 2, 1]
print(numbers[-1])
print(numbers[-2])
1
2
Methods vs. functions.
Methods are owned by the data it works for, functions are owned by the whole code.
Method invocation looks like this:
- result = data.method(arg)
The method is preceded by the name of the data that owns.
It's followed by a .
Then the name of the method
followed by parenthesis, (), which arguments can be passed into.
'Bubble sorting' is a type of sorting of lists.
When you assign a variable to a list, you are assigning it to a name, not to the contents of the list. So if you modify a list, the second variable that was assigned to it will also point to the same modified list.
So if you want to copy a list, you need to use the slice[:]
list1 = [1,2]
list2 = list1[:]
The above list slice function will assign the contents of all of list1 to list2.
You can also put an argument before and after the colon. [2:10]
In the above case, it will only copy the list elements from index 2 (inclusive to index 10 (not inclusive).
You can also use the 'del' instruction to delete a slice of the list:
list1 = [1,2,3,4,5,6,7]
del list1[2:6]
print list1
'[1,2,7]'
Thanks for reading my notes on what I learned today. Niall