Welcome to your introduction to Python!
This guide covers the fundamental building blocks of
the language, organized to help you get coding quickly.
1.1 Python Fundamentals: Tokens & Variables
Before writing complex logic, you need to understand the basic "grammar" of Python.
* Character Set: Python supports the full Unicode character set, which includes letters (A-Z,
a-z), digits (0-9), and special symbols like +, -, _, etc.
* Tokens: These are the smallest individual units in a python program.
* Keywords: Reserved words (e.g., if, while, True, None).
* Identifiers: Names given to variables, functions, or classes.
* Literals: Fixed values (e.g., 5, "Hello", 3.14).
* Operators: Symbols like +, ==.
* Punctuators: Symbols used for structure (e.g., :, ( ), [ ]).
Lvalue and Rvalue
In an assignment like x = 10:
* Lvalue (Left Value): The memory location or identifier where data is stored (e.g., x). It must be
able to hold a value.
* Rvalue (Right Value): The actual data or expression being assigned (e.g., 10).
Comments
Comments are ignored by the Python interpreter and are used to explain code.
* Single-line: Use the hash symbol #.
* Multi-line: Use triple quotes ''' or """.
1.2 Data Types & Mutability
Python categorizes data into several types. A key distinction is whether a type is mutable (can
be changed after creation) or immutable (cannot be changed).
| Category | Type | Description | Mutability |
|---|---|---|---|
| Numbers | int, float, complex | Whole numbers, decimals, and a + bj forms. | Immutable |
| Boolean | bool | True or False. | Immutable |
| None | NoneType | Represents the absence of a value. | Immutable |
| Sequence | str | Textual data (strings). | Immutable |
| Sequence | tuple | Ordered collection of items. | Immutable |
| Sequence | list | Ordered, flexible collection. | Mutable |
| Mapping | dict | Key-value pairs (e.g., {"name": "Gemini"}). | Mutable |
1.3 Operators and Expressions
Operators perform actions on variables and values.
* Arithmetic: +, -, *, /, // (floor division), % (modulus), ** (exponent).
* Relational: ==, !=, >, <, >=, <=.
* Logical: and, or, not.
* Assignment: =, and Augmented versions like +=, -=, *=.
* Example: x += 5 is shorthand for x = x + 5.
Precedence and Evaluation
When multiple operators appear in one expression, Python follows a specific order (similar to
PEMDAS):
* Parentheses ()
* Exponentiation **
* Multiplication/Division *, /, //, %
* Addition/Subtraction +, -
* Relational/Comparison
* Logical operators (not → and → or)
Type Conversion
* Implicit: Python automatically converts types (e.g., adding an int to a float results in a float).
* Explicit (Type Casting): You manually convert types using functions like int(), float(), or str().
Input and Output
* Input: Use input("Prompt"). Note that input() always returns a string.
* Output: Use print().
Would you like me to create a practice quiz on these concepts or provide some code snippets
for you to try running?