Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 859 Bytes

python_basics.md

File metadata and controls

41 lines (31 loc) · 859 Bytes

for loops

Iterate over list

fruit = ["apple", "orange", "grape", "banana", "kiwi"]
for f in fruit:
    print(f)

Iterate over list with index

fruit = ["apple", "orange", "grape", "banana", "kiwi"]
for i, f in enumerate(fruit):
    print(f"{i}: {f}")

Iterate over two lists in parallel

fruit = ["apple", "orange", "grape", "banana", "kiwi"]
veggie = ["carrot", "tomato", "bean", "pea"]
for f, v in zip(fruit, veggie):
    print(f"{f}, {v}")

Types

Python is dynamically typed. That means the types are evaluated at runtime. That is why type errors are a kind of exception.

Adding type hint to your code just helps to clarify it. The interpreter actually does not look at them.

Example:

def f(text: str, number: int) -> str:
    print(number)
    inner: int = number
    return str