Python Elements and Syntax
1. Basic Elements
- 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.
Variable
- Variables are containers for storing data values. Python is dynamically typed, meaning you don’t need to declare the type of a variable explicitly.
Data Types
- Numeric:
int
, float
, complex
- Text:
str
- Sequence:
list
, tuple
, range
- Mapping:
dict
- Set Types:
set
, frozenset
- Boolean:
bool
- Binary:
bytes
, bytearray
, memoryview
String
- 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()
List
- 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)
Tuple
- 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.
Dictionary
- 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()
Set
- 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 (^
)
2. Special Characters
Square Brackets []
:
- Lists and Indexing: Used to create lists and access elements within sequences.
Curly Braces {}
:
- Dictionaries and Sets: Used to create dictionaries and sets.
Parentheses ()
:
- Function Calls and Tuples: Used to call functions and create tuples.
Colon :
:
- Slicing, Function Definitions, and Control Flow: Used in slicing, defining functions, and indicating the start of a code block.
Comma ,
:
- Element Separator: Used to separate elements in lists, tuples, and function arguments.
Ellipsis ...
:
- Continuation: Indicates continuation in code, often used in slicing.
3. Accessing and Manipulating Elements
Indices:
- Accessing Elements in Lists and Tuples: Access elements using indices. Python supports negative indexing to access elements from the end of the list.
Slicing:
- Extracting Subsets: Slice lists and strings using
start:stop:step
.
Modifying Elements:
- Lists and Dictionaries: Lists and dictionaries are mutable, allowing you to change, add, or remove elements.
4. Case and Space Sensitivity
Case Sensitivity:
- Variable and Function Names: Python is case-sensitive, meaning
VariableName
and variablename
are different identifiers.
Space Sensitivity:
- Indentation: Python uses indentation to define code blocks. Consistent use of spaces (or tabs) is crucial for correct code execution.
5. Code Suggestions and Completions
Interactive Development:
- 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.
6. Scripts vs Functions
Scripts:
- 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.
Functions:
- Modular Code: Functions are blocks of reusable code defined using the
def
keyword. Functions allow for modularity, encapsulation, and code reuse.
7. Command Cheat Sheet
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(): |
8. Suggested Tutorials
9. Supplemental Materials