Python Intermediate
Summary Table
Summary of the Python Intermediate section.
Concept | Example | Description |
---|---|---|
Zero-based index | data[0] |
Access the first element or character |
Negative index | data[-1] |
Access the last element or character |
Nested indexing | data[0][1] |
Access an element or character within a nested structure |
String indexing | "Text"[0] |
Access a character in a string |
Slice range | data[2:10] |
Elements or characters from index 2 up to (not including) 10 |
Slice from start | data[:3] |
First 3 elements or characters |
Slice to end | data[4:] |
Elements or characters from index 4 to the end |
Full slice | data[:] |
Copy all elements or characters |
Reversing | data[::-1] |
Reverse the list or string |
if |
if condition: |
Run block only if condition is True |
else |
else: |
Run if all previous conditions are False |
elif |
elif condition: |
Additional check after if |
Logical Operators | and , or , not |
Combine or negate multiple conditions |
Nested Conditional Statements | if ... if ... |
Place one conditional inside another |
Truthy/Falsy | if [] , if "text" |
Empty values are False , non-empty are True |
for Loops |
for item in sequence: |
Iterate over elements in a sequence |
List Comprehension | [x for x in sequence] |
Create a list using a loop in one line |
Dictionary Comprehension | {k: v for k, v in pairs} |
Create a dictionary using a loop in one line |
while Loops |
while condition: |
Loop while a condition is True |
break |
if condition: break |
Exit the loop immediately |
continue |
if condition: continue |
Skip the current loop iteration |
enumerate() |
for i, val in enumerate(seq): |
Loop with index and item |
zip() |
for a, b in zip(list1, list2): |
Loop over multiple sequences in parallel |
Infinite Loop | while True: |
Runs forever unless externally stopped |