2. Python Basics#
Python fundamentals set the stage for the data work that follows in this book. In this chapter, you will use Python to represent values, apply decision rules, repeat work, organize collections, clean and format text, and package repeated logic into functions.
Variables and Types: A variable names a value, and the value’s type (int, float, str, bool) determines which operations are allowed.
Conditionals and Loops:
ifstatements andforandwhileloops let a program make decisions and repeat work.Lists, Tuples, and Dictionaries: Lists hold ordered values found by position, tuples hold fixed groups of values that cannot change, and dictionaries hold key-value pairs found by key.
Strings: String methods such as
strip(),lower(),split(), andjoin()clean and parse text, and f-string format specifiers display numbers as currency and percentages.Mutability and Aliasing: Lists and dictionaries can change in place, so two names for the same object can affect each other.
Functions: Parameters and return values package repeated logic so a calculation can be reused and checked.
The chapter keeps the focus on practical fluency for data science and management work. You are not expected to become a software engineer here. The goal is to become comfortable enough with Python to read, modify, and write small programs that support later work with NumPy, pandas, visualization, and statistical simulations.
Chapter Video
Video credit: freeCodeCamp.org. (2018, July 11). Learn Python - Full Course for Beginners [Tutorial]. YouTube. Watch on YouTube (4:26:52).
This embedded video is provided for educational reference only. All rights are retained by the respective copyright holder.
Learning Objectives
By the end of this chapter, you will be able to:
Write and run simple Python statements that use correct syntax, variables, objects, types, and operators.
Use conditional statements and Boolean expressions to represent business rules and decision logic.
Use
whileandforloops to repeat work over counts, conditions, and collections.Create and work with lists, tuples, and dictionaries to organize, access, modify, and count data values.
Clean, parse, and format text data with string indexing, string methods,
split(),join(), and f-string format specifiers.Define, call, and debug functions with parameters, arguments, return values, docstrings, scope, and tracebacks.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Syntax |
The formal rules that define how Python statements must be written. |
2 |
Statement |
A line or unit of code that tells Python to perform an action. |
3 |
Expression |
A piece of code that produces a value. |
4 |
Variable |
A name that refers to a value stored by a program. |
5 |
Assignment statement |
A statement that connects a name to a value. |
6 |
Object |
A value in Python, such as a number, string, list, dictionary, or function. |
7 |
Value |
The information stored in an object, such as |
8 |
Type |
A category of value that determines what operations Python can perform, such as |
9 |
Dynamic typing |
Python behavior where a variable name can refer to objects of different types over time. |
10 |
Type conversion |
Changing a value from one type to another, such as converting a string to an integer. |
11 |
Function |
A reusable block of code that can take inputs, perform work, and return a result. |
12 |
Function call |
An instruction that runs a function by using its name followed by parentheses. |
13 |
Argument |
A value passed to a function when the function is called. |
14 |
Parameter |
A variable name listed in a function definition. |
15 |
Return value |
The value a function sends back to the code that called it. |
16 |
Docstring |
A short string at the start of a function that explains what the function does. |
17 |
Module |
A Python file or library that groups reusable code. |
18 |
Package |
A collection of modules organized for reuse. |
19 |
Keyword |
A reserved word in Python with a special meaning, such as |
20 |
Operator |
A symbol or word that performs an operation, such as |
21 |
Operator precedence |
The order Python follows when evaluating operations in an expression. |
22 |
Boolean value |
One of the two truth values, |
23 |
Boolean expression |
An expression that evaluates to |
24 |
Conditional statement |
Code that runs different instructions depending on whether a condition is true or false. |
25 |
Branch |
One possible path through a conditional statement. |
26 |
Nested conditional |
A conditional statement placed inside another conditional statement. |
27 |
Loop |
Code that repeats a block of instructions. |
28 |
Iteration |
One repetition of a loop body. |
29 |
|
A loop that repeats while a condition remains true. |
30 |
|
A loop that repeats once for each item in a sequence or other iterable object. |
31 |
Sentinel value |
A special value that signals when a loop should stop. |
32 |
Infinite loop |
A loop that does not stop unless interrupted or corrected. |
33 |
Sequence |
An ordered collection whose items can be accessed by position, such as a string, list, or tuple. |
34 |
List |
An ordered, mutable collection of values that can be indexed, sliced, modified, and iterated over. |
35 |
Index |
A position number used to access an item in a sequence. |
36 |
Slice |
A selected range of items from a sequence. |
37 |
Mutability |
The ability of an object, such as a list, to be changed after it is created. |
38 |
Method |
A function that belongs to an object, such as |
39 |
Aliasing |
A situation where two or more names refer to the same object. |
40 |
Tuple |
An ordered, immutable sequence of values, often used for a small fixed record such as a product and its price. |
41 |
Immutable |
Unable to be changed after creation; strings and tuples are immutable. |
42 |
Tuple unpacking |
Assigning the values in a tuple or other sequence to several names at once, as in |
43 |
|
A built-in function that pairs the elements of two or more sequences into tuples. |
44 |
String |
A sequence of characters used to store text. |
45 |
String method |
A function that belongs to a string, such as |
46 |
|
A string method that removes spaces, tabs, and newlines from both ends of a string. |
47 |
|
A string method that breaks a string into a list of pieces at a delimiter. |
48 |
|
A string method, called on a delimiter, that combines a list of strings into one string. |
49 |
Delimiter |
A character or string that marks the boundary between pieces of text, such as a comma or a hyphen. |
50 |
Format specifier |
Instructions after a colon in an f-string field, such as |
51 |
Dictionary |
A mutable collection that maps keys to values. |
52 |
Key-value pair |
One entry in a dictionary, made from a key and the value associated with it. |
53 |
Key |
A lookup value used to access a dictionary entry. |
54 |
Hashable |
Able to be used as a dictionary key because the value has a stable hash. |
55 |
Accumulator |
A variable or collection that gathers results step by step during a loop. |
56 |
Scope |
The part of a program where a variable name can be used. |
57 |
Stack diagram |
A diagram that shows function calls and local variables while a program runs. |
58 |
Traceback |
Python’s error report that shows where an exception occurred. |