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, and package repeated logic into functions.
Syntax gives Python statements a precise structure.
Variables, objects, types, and operators let you represent and transform business values.
Conditional statements and loops express decision rules and repeated work.
Lists and dictionaries organize collections of values for analysis.
Functions make common calculations reusable and easier to inspect.
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: Learn Python - Full Course for Beginners by freeCodeCamp.org.
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 and dictionaries to organize, access, modify, and count data values.
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 |
Dictionary |
A mutable collection that maps keys to values. |
41 |
Key-value pair |
One entry in a dictionary, made from a key and the value associated with it. |
42 |
Key |
A lookup value used to access a dictionary entry. |
43 |
Hashable |
Able to be used as a dictionary key because the value has a stable hash. |
44 |
Accumulator |
A variable or collection that gathers results step by step during a loop. |
45 |
Scope |
The part of a program where a variable name can be used. |
46 |
Stack diagram |
A diagram that shows function calls and local variables while a program runs. |
47 |
Traceback |
Python’s error report that shows where an exception occurred. |