Skip to main content

Posts

Abstraction | Core Python 3.8

In this chapter, you’ll learn how to group statements into functions, which enables you to tell the computer how to do something, and to tell it only once. You won’t need to give it the same detailed instructions over and over. The chapter provides a thorough introduction to parameters and scoping, and you’ll learn what recursion is and what it can do for your programs. Laziness Is a Virtue  The programs we’ve written so far have been pretty small, but if you want to make something bigger, you’ll soon run into trouble. Consider what happens if you have written some code in one place and need to use it in another place as well. For example, let’s say you wrote a snippet of code that computed some Fibonacci numbers (a series of numbers in which each number is the sum of the two previous ones). fibs = [0, 1]  for i in range(8):     fibs.append(fibs[-2] + fibs[-1]) After running this, fibs contains the first ten Fibonacci numbers. >>> fibs...

Conditionals, Loops, and Some Other Statements | Core Python 3.8

By now, I’m sure you are getting a bit impatient. All right—all these data types are just dandy, but you can’t really do much with them, can you? Let’s crank up the pace a bit. We’ve already encountered a few statement types (print statements, import statements, and assignments). Let’s first take a look at some more ways of using these before diving into the world of conditionals and loops. Then we’ll see how list comprehensions work almost like conditionals and loops, even though they are expressions, and finally we’ll take a look at pass, del, and exec. More About print and import  As you learn more about Python, you may notice that some aspects of Python that you thought you knew have hidden features just waiting to pleasantly surprise you. Let’s take a look at a couple of such nice features in print and import. Though print is really a function, it used to be a statement type of its own, which is why I’m discussing it here.  ■ Tip :  For many applications, loggin...