Reusing and Organizing Code in Python
00:00 As programs grow, structure and code reuse become essential. In this lesson, you’ll explore conditionals, loops, functions, classes, and imports.
00:12 Conditionals let your program decide what code to run based on some existing state.
00:17
Consider this code snippet. With age set to 18, the if block runs if its condition is True. Is age greater than or equal to 16?
00:27
Otherwise, the else block runs. Because 18 is greater than 16, the code snippet prints, "You can drive". It’s important to note that indentation is what defines each code block.
00:40
Loops let you repeat a block of code without writing it over and over. Python provides two main kinds of loops. for loops iterate over the items in a collection.
00:49
while loops repeat as long as some condition remains true. Just like conditionals, indentation determines which lines belong to a loop.
00:59
On the left, the for loop, for name in the list of strings, "Ada", "Grace", and "Guido", the loop assigns each item in the list to name and then prints inside the loop.
01:09
The iteration stops after processing the final item, moving on to the next line of code. The while loop starts with the count variable set to 3, and tests the condition count greater than 0.
01:20
It repeats while that resolves to true. And importantly, the loop body should decrease count after each iteration. Otherwise, this would run forever.
01:30 Probably not the intended result.
01:32 Ultimately, 3, 2, and 1 are printed. Then the loop stops.
01:38
Functions package reusable behavior. You’ve seen built-in functions like len() and print(). Well, you can also define your own.
01:45
You define a function with the def keyword followed by the function name and a pair of parentheses. Within the parentheses are the optional arguments.
01:53
Data to be passed to the function. After the function has performed its operations, it can send data back with the return keyword. Once defined, you call a function using parentheses, passing in the appropriate arguments.
02:06 Going even deeper now, we have classes. Classes let you define your own types of objects, bundling behavior and data using methods and attributes. They’re foundational to object-oriented programming in Python.
02:20
.__init__() sets up each new object and stores data on self. Methods are functions defining behaviors that objects created from the class can perform.
02:29 You create instances by calling the class with the appropriate arguments.
02:34
The Dog class, shown below, stores a .name attribute and a .speak() method. Calling Dog and passing in the string "Fido" returns an instance stored in the fido variable.
02:43
Printing the result of calling fido.speak() returns the f-string found in the .speak() method, using the fido instance’s data.
02:53 And going beyond the current file, imports let you bring in code from other modules. Python is bundled with a standard library full of useful modules. That’s why we say Python comes batteries included.
03:05
One of these is the math module, with, you guessed it, a bunch of mathematical constants and functions. So you can import math and use dot notation to call math.sqrt() to get the square root of 16, 4.
03:18
Or you can access the constant pi, with math.pi.
03:24 Once again, we’ve but scratched the surface. Learn more about loops and conditionals, and even more, with Control Flow Structures in Python. Dive deep into functions with Defining and Calling Python Functions.
03:36 Begin to explore classes further in Intro to Object-Oriented Programming in Python. And in the next lesson, let’s look at what happens when Python runs into a problem.
Become a Member to join the conversation.