Python Crash Course
Welcome to the Python Crash Course on the New Boston channel. This tutorial will cover a wide range of topics including syntax, variables, data types, numbers, strings, booleans, operators, loops, functions, classes, and objects.
Comments
- Used to explain code or make it more readable
- Start with a
#
- Can prevent code execution
- Single line or multi-line (triple quotes)
Variables
- Created by assigning values
- Types: numbers, strings, etc.
- Type can be changed using casting (e.g.,
str(10)
, int('3')
, float(3)
)
Data Types
- Strings: Single or double quotes, immutable, accessed by indices
- Integers & Floats: Whole numbers vs. decimal numbers
- Booleans:
True
or False
- Use
type()
to check data type
- Lists: Ordered, changeable, allow duplicates, defined with square brackets
- Tuples: Ordered, unchangeable, allow duplicates, defined with round brackets
- Sets: Unordered, unique items, defined with curly braces
- Dictionaries: Key-value pairs, unordered, changeable, keys must be unique, defined with curly braces
Operators
- Arithmetic:
+
, -
, *
, /
, %
- Assignment:
+=
, -=
, *=
, /=
, %=
, etc.
- Comparison:
==
, !=
, >
, <
, >=
, <=
Conditional Statements
- if, elif, else
- Indentation is crucial
- Boolean context for conditions
Loops
- While loops: Repeat as long as a condition is true
- For loops: Iterates over a sequence
- Break & Continue
- Else with loops
Functions
- Defined using
def
keyword
- Can take parameters and return values
- Syntax:
def functionName(parameters):
Classes & Objects
- Python is object-oriented
- Define classes using
class
keyword
- Objects are instances of classes
Conclusion
- Encouragement to subscribe and interact
- Offered to provide more comprehensive tutorials if desired