Python in 5 Weeks: Python Programming for Beginners—with Interactivity
Published by O'Reilly Media, Inc.
Python programming for beginners
Learning a programming language is like learning a foreign (human) language: It requires not just a new mindset, but also lots of practice in using the language. But if you’ve never programmed before, then it’s like learning a foreign language without first knowing what nouns and verbs are. You need to learn not only the syntax, but also the fundamental concepts behind programming.
No wonder learning to program frustrates so many people — you’re trying to scale two ladders at once, learning new syntax and new ideas at the same time. Moreover, many classes are aimed at people who have previously used another programming language. The pace of such classes isn’t appropriate for people just starting on their programming journey.
This five part course will help you to take your first steps as a programmer, at a pace and with content aimed squarely at people who haven’t coded before. (Or who took a university course, and decided that coding wasn’t for them.) We’ll do it using Python, a language that is not just used in industry, but is famous for its relatively shallow learning curve. You’ll learn to write real programs — small and simple programs, but real ones nonetheless — using the same techniques and syntax as professional developers use every day, in commercial and open-source projects all over the world. And you’ll understand some of the decisions that programmers have to make every day, as they design the software we all use.
Week 1: Fundamentals and Core Concepts
In this first week, Reuven will help you to take your first steps as a programmer, at a pace and with content aimed squarely at people who haven’t coded before. (Or who took a university course, and decided that coding wasn’t for them.) He’ll do it using Python, a language that is not just used in industry, but is famous for its relatively shallow learning curve. You’ll learn to write real programs — small and simple programs, but real ones nonetheless — using the same techniques and syntax as professional developers use every day, in commercial and open-source projects all over the world. And you’ll understand some of the decisions that programmers have to make every day, as they design the software we all use.
Week 2: Loops, Lists, and Tuples
Loops, lists, and tuples can make your Python programs more expressive, readable, and maintainable. But those new to Python often have questions: What’s the difference between for loops and while loops? Or between lists and strings? Likewise, what’s the difference between immutable data (e.g., strings) and mutable data (e.g., lists), and when do you use each? And why do tuples exist in Python anyway?
Reuven will help build up your foundational knowledge of loops, lists, and tuples. Not only will you get hands-on experience with Python syntax—you’ll also learn the underlying reasons for it, so that you actually understand these concepts front to back. You’ll begin by digging into loops, a common way to avoid violating the “don’t repeat yourself” (DRY) rule of programming, as you learn how to use a for loop to iterate over the elements of a string or perform an action a certain number of times. You’ll then turn to the list, a type of collection that lets you hold onto several other pieces of data. Whether it’s a collection of usernames, a collection of IP addresses, or even a collection of earnings reports, lists are Python’s go-to data structure. You’ll end with a quick look at tuples, a core data structure in Python...but one that beginners can largely ignore, at least at first.
Week 3: Dictionaries and Files
In this course, we’ll look at two crucial aspects of Python development: Dictionaries and files.
First, we’ll look at dictionaries, the crown jewels of Python data structures. Dictionaries make it easy for us to create complex data structures and search through our data quickly. We’ll talk about how to create dictionaries, modify them, and retrieve data from them. We’ll also discover how flexible they are, and even peek a bit behind the scenes to understand why they work the way they do.
Next, we’ll look at files — how we can read data into our program from files (such as from logfiles or configuration files), and how we can write data from our program back out to files (such as simple reports and other output). Whether we’re reading logfiles or configuration files, or writing simple reports that others can read,
Week 4: Functions
In previous installments, we learned how to use the built-in data types and functions to organize our data and perform many useful operations. But we still haven’t done one of the most important parts of programming, namely defining our own functions. Functions provide us with numerous advantages: They cut down on the amount of code we need to write, they allow us to think at a higher level of abstraction, and they let us create a specialized vocabulary within our code, one which expresses our intentions clearly.
In this course, we’ll learn what functions are, why we want to define them, and then how we do so in Python. Along the way, we’ll talk about parameters, defaults, and return types. By the end of this course, you’ll have hands-on experience writing functions and using them in a variety of contexts.
Week 5: Modules and Packages
In previous sessions of “Python First Steps,” you’ve now learned to use Python’s built-in data structures, write and execute functions, and work with files. These techniques form the foundations for all modern software; if you were to look under the hood of your favorite operating system, browser, word processor, or mobile app, you would see a huge number of data structures and functions interacting.
But what if a function or data structure will be useful across multiple applications? Rather than reinvent the wheel each time, you’ll likely want to put it in a “library,” a centralized code repository from which your code can “borrow” functionality whenever it needs. In Python, such libraries are known as “modules,” with collections of modules known as “packages.”
This week, we will look into modules and packages — how we can use the “standard library” that comes with Python, how we can download and install packages from PyPI (the Python Package Index), and how we can even write some simple modules of our own.
What you’ll learn and how you can apply it
Week 1: Fundamentals and Core Concepts
- The role of variables in programs
- What it means to have a “boolean expression”
- What sorts of comparisons we can use in programming
- How to gather input and produce output
- The importance of “if” statements
- What sorts of errors users can make
- The differences between boolean, string, and integer values
- The basic syntax and structure of Python programs
Week 2: Loops, Lists, and tuples
- When we use loops, and why
- The difference between “for” and “while” loops
- The differences (and similarities) between looping over a string and a list
- How to create lists and retrieve from them
- The difference between immutable data (e.g., strings) and mutable data (e.g., lists)
- Methods for appending to and removing from lists
- How to sort data in lists
- How to turn strings into lists with “split”
- How to turn lists into strings with “join”
- What tuples are, and where they’re used
Week 3: Dictionaries and Files
- How to decide when to use a dict, and how to do so
- How to add to, retrieve from, and query a dict
- How dicts work behind the scenes
- How to use dicts within “for” loops
- Why we need to “open”’ files before reading from or writing to them
- How to use “with” when working with a file, to guarantee it’s closed
- How we can use “for” loops to read from a file
Week 4: Functions
- When we use functions, and why
- How to define functions
- How to write function documentation with “docstrings”
- How to use parameters
- Function return values
- When (and how) to break a function into smaller pieces
Week 5: Modules and Packages
- How to use “import” and “from import”
- Why you shouldn’t use “from … import *”
- The meaning of “standard library” in Python
- How to use data structures and functions defined in modules
- The difference between modules and packages
- How to write your own module
- The meaning of “if name == ‘main’”, and how to use it in your code
- How to download and install distribution packages from PyPI
And you’ll be able to:
Week 1: Fundamentals and Core Concepts
- Assign values — including from the user — to variables
- Use Python’s “input” and “print” functions for basic input and output
- Compare values using ==, !=, <, and >
- Make decisions using “if” statements
- Combine comparisons with “and” and “or”
- Write Python programs that get input from the user, make decisions, and produce output
- Run Python programs that you have written
Week 2: Loops, Lists, and Tuples
- Use loops to write cleaner, more maintainable code
- Use loops to repeat actions, including asking for user input
- Use lists to store and retrieve a variety of information
- Take string input from the user and turn it into a list (for processing)
- Take a list and turn it into a string (for display)
- Sort items in a list, and then get the smallest or largest value
Week 3: Dictionaries and Files
- Organize your programs’ data better with dictionaries
- Work with more complex data than before
- Start thinking about the efficiency of searches for your data
- Read from configuration files and logfiles
- Store data, including reports, in files
- Get a list of files from a directory, and do something with each file
Week 4: Functions
- Write functions that encapsulate repeated and useful functionality
- Write a single function that takes a variety of argument types
- Determine when to use function defaults
- Determine the difference between local and global variables
- Recognize how to combine unpacking and return values
Week 5: Modules and Packages
- Do more with less code, using modules in the standard library
- Selectively import names from a module via “from… import”
- Download and use distribution packages from PyPI
- Write simple modules
This live event is for you because...
- You haven’t ever programmed before, and you need to learn the basics.
- You’re a system or network administrator who wants to automate your job beyond simple scripts with Python but aren’t sure where to start.
- You manage developers and would like to have a better idea of what they do.
- You use Excel in your work and would like to use pandas—but first need to learn how to program in Python.
- You want to become a developer or data scientist, and know that Python is a major player, but aren’t sure where to start with development.
Prerequisites
- No prior programming experience necessary.
Recommended preparation:
- Read Chapter 1 “Thinking Computationally: Getting Started” in Head First Learn to Code
- Explore Python Cookbook: Strings & Text (Katacoda interactive scenario)
- The source of live-coded examples will be provided as Jupyter notebooks, so, optionally, you may want to preinstall Jupyter on your local machine, or set up a free account with Google Colab.
Recommended follow-up:
- Read Introducing Python, 2nd Edition (book)
- Read Head First Python, 2nd Edition (book)
Schedule
The time frames are only estimates and may vary according to how the class is progressing.
Week 1: Fundamentals and Core Concepts (240 minutes / 4 hours)
What is a programming language? What is Python? (15 minutes)
- Lecture: What do programming languages help us to do? Why are there so many? Where does Python fit into this constellation? Why is Python so popular today?
- Q&A
Using Jupyter (15 minutes)
- Lecture: What is Jupyter? Basic use and navigation. Running code. Editing code. Getting results.
- Q&A
Basic ints and strings. Assignment. Variables. +. (30 minutes)
- Lecture: Data and data types. Variables. Assigning to variables. Different data types are different — why? Expressions.
- Exercises: Simple calculator. Simple greeting.
- Q&A
- 5-minute break
Input from the user. “input” always returns a string. Assigning to a variable. Printing. Simple f-strings. (30 minutes)
- Lecture: Using the “input” function to get input from the user. Assigning that input to a variable. Printing strings.
- Exercise: Displaying friendly greetings.
- Q&A
== and expressions. True and False. Comparisons with < and >. If/else. (30 minutes)
- Lecture: How do we compare two values in Python? Comparing with ==, <, and >. The structure of an “if” statement. Indentation. The “else” clause.
- Exercise: Which word comes first?
- Q&A
- 5-minute break
Elif and alternatives. “or” and “and”. “not”. (30 minutes)
- Lecture: More complex comparisons with “elif” clauses. Logical alternatives with “and”, “or”, and “not”.
- Exercise: Name and city.
- Q&A
== and numeric expressions. True and False. If/else. (30 minutes)
- Lecture: Turning strings into numbers. Comparing numbers. Numbers in “if/else” expressions.
- Exercise: Number guessing game.
- Q&A
- 5-minute break
Creating strings. Retrieving from strings with indexes and slices. (30 minutes)
- Lecture: Grabbing parts of a string with an index or slice. Using “in” to search in a string.
- Exercise: Grabbing a piece of a string
- Q&A
Final project/exercise (25 minutes)
- Discussion: Final exercise
- Exercise: Pig Latin
- Q&A
- Exercise: Complete course survey
Week 2: Loops, Lists, and Tuples
Introduction (10 minutes)
- Presentation: Introduction to Week 2 topics
- Q&A
Intro to “for” loops (25 minutes)
- Presentation: When to use “for” loops; how they work
- Hands-on exercise: Create a vowel and consonant counter
- Q&A
“for” loops and range (25 minutes)
- Presentation: How to repeat a task a set number of times
- Hands-on exercise: Name triangles
- Q&A
- Break
“while” loops (25 minutes)
- Presentation: How “while” loops are different from “for” loops; when to use them instead
- Hands-on exercise: Sum numbers
- Q&A
Where’s the index? (25 minutes)
- Presentation: Manual and automatic solutions
- Hands-on exercise: Powers of 10
- Q&A
- Break
Intro to lists (25 minutes)
- Presentation: What is a list?; how to create and add to lists
- Hands-on exercise: Odds, evens, and others
- Q&A
Lists can hold anything. (25 minutes)
- Presentation: Why lists are a generic container
- Hands-on exercise: Lists
- Q&A
Mutability versus immutability (15 minutes)
- Presentation: How data structures in Python are either mutable or immutable
- Q&A
- Break
Splitting strings into lists (25 minutes)
- Presentation: How to take input (from the user or elsewhere) and turn a single string into a list of strings
- Hands-on exercise: Sum numbers while ignoring others
- Q&A
Joining lists into strings (25 minutes)
- Presentation: How to take a list and put it into a string
- Hands-on exercise: Write a Pig Latin sentence
- Q&A
Tuples (15 minutes)
- Presentation: What are tuples?
- Q&A
Week 3: Dictionaries and Files (240 Minutes / 4 hours)
The story so far: A quick recap of simple data structures (10 minutes)
- Presentation: Recap of topics
- Q&A
Simple dictionaries: Defining, retrieving, and searching (30 minutes)
- Presentation: Simple dictionaries: Defining, retrieving, and searching
- Exercise: restaurant
- Q&A: 5 minutes
Dictionaries are mutable (15 minutes)
- Presentation: Dictionaries are mutable
- Q&A
- 5-minute break
Accumulating in dictionaries (30 minutes)
- Presentation: Accumulating in dictionaries
- Exercise: vowels, digits, and others
- Q&A
Accumulating the unknown (25 minutes)
- Presentation: Accumulating the unknown
- Exercise: rainfall
- Q&A
- 5-minute break
Looping over dicts (15 minutes)
- Presentation: Looping over dicts
- Q&A
How do dicts work? (15 minutes)
- Presentation: How do dicts work?
- Q&A
Intro to files (25 minutes)
- Presentation: Intro to files
- Exercise: summing numbers
- Q&A
- 5-minute break
More reading from files (25 minutes)
- Presentation: More reading from files
- Exercise: IP addresses to dict
- Q&A
Writing to files + the “with” construct (30 minutes)
- Presentation: Writing to files + the “with” construct
- Exercise: dict to config file
- Q&A
Week 4: Functions
What are functions? Nouns vs. verbs in programming (10 minutes)
- Presentation: What are functions?
- Q&A
Writing simple functions (20 minutes)
- Presentation: Writing simple functions
- Exercise: calculator
- Q&A
Arguments and parameters (25 minutes)
- Presentation: Arguments and parameters
- Exercise: mysum
- Q&A
- 5-minute break
Return values (25 minutes)
- Presentation: Return values
- Exercise: Biggest and smallest
- Q&A
Default argument values (25 minutes)
- Presentation: Default argument values
- Exercise: all_lines
- Q&A
- 5-minute break
Complex return values (25 minutes)
- Presentation: Complex return values
- Exercise: file info
- Q&A
Unpacking (25 minutes)
- Presentation: Unpacking
- Exercise: This, that, or the other
- Q&A
- Survey: 5 minutes
Local vs. global variables (25 minutes)
- Presentation: Local vs. global variables
- Exercise: factorial
- Q&A
More on locals vs. globals (25 minutes)
- Presentation: More on locals vs. globals
- Exercise: call tracker
- Q&A
Tuples (10 minutes)
- Presentation: Tuples
- Q&A
Week 4 Challenge
- Python Challenge: Functions Python Challenge: Functions
Week 5: Modules and Packages
Intro to modules (30 minutes)
- Review of challenge
- Presentation: Intro to modules
- Exercise: hello from the module
- Q&A
Modules can contain (25 minutes)
- Presentation: Modules can contain
- Exercise: Modules
- Q&A
- 5-minute break
The different forms of “import” (30 minutes)
- Presentation: The different forms of “import”
- Exercise: Importing and re-importing
- Q&A
Developing a module (25 minutes)
- Presentation: Developing a module
- Exercise: menu
- Q&A
- 5-minute break
Python’s standard library (30 minutes)
- Presentation: Python’s standard library
- Exercise: hello from the module
- Q&A
Modules vs. packages (25 minutes)
- Presentation: Modules vs. packages
- Exercise: Modules vs. packages
- Q&A
- 5-minute break
Intro to PyPI (and “awesome Python”) (30 minutes)
- Presentation: Intro to PyPI
- Exercise: finding a good module
- Q&A
Using “pip” (25 minutes)
- Presentation: Using “pip”
- Exercise: Installing and using “requests”
- Q&A
Your Instructor
Reuven M. Lerner
Reuven M. Lerner is a full-time Python trainer. In a given year, he teaches courses at companies in the United States, Europe, Israel, India, and China—as well as to people around the world via his online courses. Reuven created one of the first 100 websites in the world just after graduating from MIT’s Computer Science Department. He opened Lerner Consulting in 1995 and has been offering training services since 1996. Reuven’s monthly column appeared in Linux Journal from 1996 until the magazine’s demise in 2019. He’s a panelist on the Business of Freelancing podcast and previously spent five years as a panelist on the Freelancers Show podcast. Reuven’s most recent book is Python Workout (Manning), a collection of Python hands-on exercises with extensive explanations. His free weekly Better Developers newsletter, about Python and software engineering, is read by more than 20,000 developers around the globe. His Trainer Weekly newsletter is similarly popular among people who give corporate training. Reuven has a bachelor’s degree in computer science and engineering from MIT and a PhD in learning sciences from Northwestern University. He lives in Modi’in, Israel, with his wife and three children.