- Single-line comments: Created using the hash symbol (
#). Everything after#on that line is ignored by the Python interpreter. - Multi-line comments: Though Python does not have a built-in syntax for multi-line comments, you can use multi-line strings (enclosed in triple quotes
'''or""") as a workaround.
- Variables are containers for storing data values. Python is dynamically typed, meaning you don’t need to declare the type of a variable explicitly.
- Numeric:
int,float,complex - Text:
str - Sequence:
list,tuple,range - Mapping:
dict - Set Types:
set,frozenset - Boolean:
bool - Binary:
bytes,bytearray,memoryview
- Strings in Python are immutable sequences of Unicode characters. You can create them using single or double quotes.
- String operations:
- Concatenation:
'Hello' + 'World' - Repetition:
'Hello' * 3 - Slicing:
greeting[1:5] - String methods:
greeting.upper(),greeting.lower()
- Concatenation:
- Lists are mutable, ordered collections of items, allowing mixed data types.
- List operations:
- Accessing elements:
fruits[0] - Modifying elements:
fruits[1] = 'blueberry' - List methods:
fruits.append('orange'),fruits.remove('banana'),len(fruits)
- Accessing elements:
- Tuples are immutable, ordered collections of items, often used for fixed data.
- Tuple operations:
- Accessing elements:
coordinates[0] - Tuples do not support item assignment as they are immutable.
- Accessing elements:
- Dictionaries are unordered collections of key-value pairs. Keys must be unique and immutable.
- Dictionary operations:
- Accessing values:
person["name"] - Adding/changing entries:
person["age"] = 31 - Dictionary methods:
person.keys(),person.values(),person.items()
- Accessing values:
- Sets are unordered collections of unique elements.
- Set operations:
- Adding elements:
unique_numbers.add(5) - Removing elements:
unique_numbers.remove(2) - Set operations: union (
|), intersection (&), difference (-), symmetric difference (^)
- Adding elements:
- Lists and Indexing: Used to create lists and access elements within sequences.
- Dictionaries and Sets: Used to create dictionaries and sets.
- Function Calls and Tuples: Used to call functions and create tuples.
- Slicing, Function Definitions, and Control Flow: Used in slicing, defining functions, and indicating the start of a code block.
- Element Separator: Used to separate elements in lists, tuples, and function arguments.
- Continuation: Indicates continuation in code, often used in slicing.
Below are some of the key special characters you should be familiar with:
| Character | Description |
|---|---|
[] |
Used to create lists and access list elements |
{} |
Used to create dictionaries |
() |
Used to group expressions and call functions |
: |
Used in function definitions, loops, and slicing |
# |
Used for comments |
= |
Assignment operator |
+, -, *, /, //, %, ** |
Arithmetic operators |
==, !=, <, >, <=, >= |
Comparison operators |
and, or, not |
Logical operators |
- Accessing Elements in Lists and Tuples: Access elements using indices. Python supports negative indexing to access elements from the end of the list.
- Extracting Subsets: Slice lists and strings using
start:stop:step.
- Lists and Dictionaries: Lists and dictionaries are mutable, allowing you to change, add, or remove elements.
- Variable and Function Names: Python is case-sensitive, meaning
VariableNameandvariablenameare different identifiers.
- Indentation: Python uses indentation to define code blocks. Consistent use of spaces (or tabs) is crucial for correct code execution.
- Real-time Syntax Checking: Tools like Jupyter Notebooks and IDEs such as PyCharm and VS Code offer real-time syntax checking and code suggestions.
- Auto-completion: As you type, Python IDEs can suggest function names, variable names, and syntax completions, making coding more efficient.
- Running Code Directly: Python scripts are files containing Python code that are executed directly. They are typically used for automating tasks or running a series of commands.
- Modular Code: Functions are blocks of reusable code defined using the
defkeyword. Functions allow for modularity, encapsulation, and code reuse.
| Operation | Command |
|---|---|
| Print to Console | print("Hello, World!") |
| Length of List | len(my_list) |
| Add to List | my_list.append(10) |
| Remove from List | my_list.remove(10) |
| Access Dictionary Key | my_dict["key"] |
| Check Type | type(variable) |
| Convert to String | str(100) |
| Convert to Integer | int("100") |
| Looping | for i in range(5): |
| Conditional | if x > y: |
| Function Definition | def my_function(): |