Python It’s Magic Lesson 3
No it’s just dynamic typing
In the last few weeks. we have taken a brief look at strings, integers,floats,expressions,boolean expressions, unicode symbols, variables,tuples,lists,sets, dictionaries, modules, functions, lambdas, and files.
If you think of a computer program as doing things with stuff. What we have been looking at is the stuff that it does things with. We are now going to move on and learn statements and begin to look at what sorts of things you can do with this stuff. Before we proceed are there any concerns or question.
Question: I have a bit of experience with C++and I don’t understand how Python works. When you write A = 7. How does python know what A is? You haven’t defined A, so how does it know what it is? Its like some sort of magic.
Answer: Most computer languages use static typing in which every type of thing has a fixed identity. A program changes the identity of an object by running the program. In Python objects are subject to change dynamically. The type lives with object not with the container. There are advantages and disadvantages to each approach. With dynamic typing a program is easier to write and it is much clearer to a human. Static typing on the other hand is less clear and frequently involves extra steps but it runs much faster.
We have already looked at the simple statement print which allows us to print things to the screen. This week we are going to focus on if statements, and loops.
If statements
The If statement is the foundation of Pythons logic. If statements can run sequential so that they run one after the other. if this is True do that. If something else is True do another thing. If a third thing is True then …. and so on. Or they can run arbitrarily in a nested fashion and execute only under certain conditions. One of the things that is an integral part of Python is that Python enforces good programing practice. When you are running a block of code in any language is is good practice to indent the block so that you can tell which part goes where. In python this is enforced,
if x == 7:
print ("okay")
If you type this in idle it will automatically add the correct spacing and if you manage to create code without the spacing.if x == 7:
print ("okay")The program will stop and will generate a message saying "expected an indent block"Loops
The two types of loops in Python are for loops and while. The power of the computer is not that it can calculate but that it can do it repeatedly. Thats where loops come in.
x = 0 x gets 0
y = 0 y gets 0
while x == 0 : while x equals 0 (always)
y += 1 increment y by 1
print (y)This is an endless loop. Python is quite slow if you ran this program for a year before stopping the computer. You would end up with a number significantly bigger than 30 trillion. I have no idea how big the number would be with a language like C++ but it would be very big.Jim McAulay🍁 Says “ The best time to buy a fire extinguisher is before you need a fire extinguisher.” 😜
18 +2






