Python Programming Basics: Data Types, Variables, String Formatting, and User Input
Python Data Structures and Overview
Sequences in Python:
- Lists: Ordered, mutable sequences used to store collections of data.
- Tuples: Immutable sequences used to hold fixed collections of items.
- Ranges: Sequence types representing an immutable sequence of numbers, commonly used in loops.
Dictionaries:
- Data structure patterned after concepts used in lossless compression.
- Composed of key-value pairs.
- A single key and its associated value together form an item.
Sets:
- Identical to the mathematical definition of sets (from Set Theory / Math 2).
- Must contain unique elements only; strictly prohibits duplicate values.
Booleans:
- Logical data type representing exactly one of two values:
TrueorFalse.
- Logical data type representing exactly one of two values:
Numeric Types:
- Integers (
int): Whole numbers without decimal points (e.g., , ). - Floating-point numbers (
float): Real numbers containing decimal points (e.g., ).
- Integers (
Syntax Rules, Comments, and Data Type Inspection
Code Cells:
- Executable blocks containing actual Python code.
The
print()Function:- Used to output text or object values directly to the screen.
- Requires the function name
printfollowed immediately by an opening parenthesis(, parameters inside, and a closing parenthesis)as the final character on the line.
Data Type Inspection with
type():- Wrapping an object or variable inside
type()returns its exact data type. - Passing a string literal to
type()and printing it outputs<class 'str'>to the screen.
- Wrapping an object or variable inside
Comments in Python:
- A comment begins with a pound symbol / hashtag
#as the first character of a line. - Comments are human-readable notes completely ignored by the Python interpreter during code execution.
- If the
#symbol is removed from a line containing plain English text (e.g., removing#from# explore data types with type), execution fails and Python raises a syntax error because it does not recognize the unformatted plain text as valid code. - Selective uncommenting (removing
#) allows step-by-step execution line by line to inspect multiple data types sequentialy.
- A comment begins with a pound symbol / hashtag
Code Output, Strings, and Syntax Errors
String Literals:
- Text values must be enclosed inside quotation marks (e.g.,
"hello"). - Removing quotation marks from string text (e.g., writing
helloinstead of"hello") causes Python to search for a variable or identifier namedhello. - If no such variable exists, code editors flag it with a red squiggly error indicator and Python raises a
NameErrorupon execution.
- Text values must be enclosed inside quotation marks (e.g.,
Numbers vs. String Representations of Numbers:
- A bare integer like
42is of data typeint. - Wrapping a number in quotation marks (e.g.,
"42") changes its data type fromintto string (str).
- A bare integer like
Mathematical Operations vs. String Concatenation:
- Standard Addition: Evaluating numeric addition outputs standard mathematical sums, such as .
- Standard Multiplication: Evaluating numeric multiplication outputs standard mathematical products, such as .
- String Concatenation with
+:- When the
+operator is applied to two strings, Python joins or "smushes" them together end-to-end. - Evaluating
"5" + "3"treats `
- When the