Chapter 1: Python Basics
Chapter 1: Python Basics
Overview
Python is a powerful, versatile programming language perfect for data science, web development, automation, and much more. This chapter introduces fundamental Python concepts: writing procedural code, working with variables and expressions, understanding built-in types, performing mathematical operations, and using classes and objects.
By mastering these basics, you'll build a solid foundation for data science and advanced Python programming.
Learning Objectives
By the end of this chapter, you will be able to:
- Write and execute procedural Python statements
- Use variables, expressions, and Python's built-in types (list, dict, set, tuple)
- Perform arithmetic operations and type conversions
- Print formatted output using f-strings
- Understand basic object-oriented concepts with classes and objects
- Use decorators and special class methods
1.1 Write Procedural Code
Procedural Statements
Procedural statements are commands executed one line at a time. These statements can run in:
- Jupyter Notebook
- IPython shell
- Python interpreter
- Python scripts
- This interactive browser terminal (powered by Pyodide!)
Let's start with a simple list assignment:
Multiple Procedural Statements
You can unpack lists into variables and use f-strings for formatted output:
Adding Numbers
Python handles numeric operations naturally:
Adding Strings
String concatenation combines text:
Complex Statements with Loops
More complex statements can iterate over data structures:
1.2 Use Simple Expressions and Variables
Assert Statement
The assert statement validates conditions. If the condition is False, it raises an AssertionError:
Pass Statement
The pass statement is a null operation - a placeholder that does nothing:
Del Statement
The del statement deletes objects:
Return Statement
Functions use return to send values back to the caller:
Yield Statement (Generators)
The yield statement creates generators - functions that produce values one at a time:
Generators are memory-efficient because they produce values on-demand rather than storing them all in memory.
Break Statement
The break statement exits loops early:
Continue Statement
The continue statement skips to the next loop iteration:
1.3 Work with Built-In Types
Dictionary (dict)
Dictionaries store key-value pairs:
List
Lists are ordered, mutable collections:
Set
Sets store unique, unordered elements:
Tuple
Tuples are ordered, immutable collections:
1.4 Printing
Basic Printing
Use print() to output text:
F-Strings (Formatted String Literals)
F-strings provide elegant string formatting:
Print Function with Separator
The sep parameter controls what appears between printed items:
1.5 Perform Basic Math Operations
Adding and Subtracting
Multiplication with Decimals (Floats)
Decimal Precision
For financial calculations or when precision matters, use the Decimal library:
Exponents
Python provides two ways to calculate exponents:
Converting Between Numerical Types
Rounding Numbers
Python provides multiple ways to round numbers:
1.6 Use Classes and Objects with Dot Notation
Special Class Methods (len)
Classes can implement special methods (also called "dunder methods" - double underscore):
@property Decorator
The @property decorator creates read-only attributes:
@staticmethod Decorator
The @staticmethod decorator creates methods that don't need self:
Immutability with Properties
Properties act like read-only attributes:
Summary
In this chapter, you learned Python basics:
- Procedural statements: Variables, loops, conditionals
- Control flow:
break,continue,return,yield - Built-in types:
dict,list,set,tuple - Math operations: Addition, multiplication, exponents, rounding
- Classes and objects: Properties, static methods, special methods
These fundamentals form the foundation for data science, web development, and automation.
Quiz
Next Steps
Now that you understand Python basics, you're ready to dive deeper into:
- Chapter 2: Working with Strings - manipulation, formatting, and regular expressions
- Chapter 3: Python Data Structures - advanced list, dict, set operations
- Chapter 4: Data Conversion Recipes - transforming data between types
Continue your learning journey to master Python for data science!
📝 Test Your Knowledge: Chapter 1: Python Basics
Take this quiz to reinforce what you've learned in this chapter.