avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Summary

In this Python tutorial, the author discusses dynamic typing in Python, contrasts it with static typing found in languages like C++, and introduces conditional statements and loops with examples.

Abstract

The web content is the third lesson in a Python programming tutorial series, titled "Python It’s Magic Lesson 3," which focuses on the dynamic typing nature of Python in contrast to static typing used in languages such as C++. The author highlights that Python's dynamism allows objects to change types, which can make programming more intuitive but may result in slower performance compared to statically-typed languages. The lesson proceeds to introduce if statements梦幻西路体验 and their importance in controlling the flow of a program based on boolean conditions, emphasizing the necessity of proper indentation in Python. Additionally, the concept of loops (for and while) is introduced with a cautionary example of an infinite loop and the potential consequences of running it. The article concludes with a humorous fire safety analogy by Jim McAulay about proactive learning.

Opinions

  • Python's dynamic typing is seen as more human-readable and easier for writing programs, but this flexibility can lead to performance trade-offs compared to statically-typed languages like C++.
  • The author asserts that Python enforces good programming practices by requiring proper indentation of code blocks, which contributes to code readability.
  • The tutorial emphasizes the foundational role of if statements in Python's logic, allowing for conditional execution of code based on boolean expressions.
  • Loops, especially while loops, are presented as powerful features of Python that enable the repetition of tasks but require careful implementation to avoid infinite loops.
  • The author uses humor, as seen in the fire extinguisher quote, to underscore the importance of learning and understanding programming concepts before they lead to problems (or 'fires').

Python It’s Magic Lesson 3

No it’s just dynamic typing

Photo by Pierrick VAN-TROOST on Unsplash

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

Python
Dynamic Typing
Programming For Beginners
Jim Mcaulay
Illumination
Recommended from ReadMedium