Home › Programming › Python
Programming
Python
Python is a clear, readable language that is a popular first language and is used everywhere from small scripts to large applications.
Variables and types
No type declarations: you just assign. The basics are numbers, strings, lists, dictionaries, and booleans.
name = 'Ada'
count = 0
items = [1, 2, 3]
user = {'name': name, 'age': 36}
Control flow
Indentation defines blocks, there are no braces. if/elif/else for choices, for and while for loops.
for n in range(5):
if n % 2 == 0:
print(n, "is even")
Functions
Define reusable code with def.
def greet(name):
return f"Hello, {name}!"
print(greet("world"))
Files
Open a file with with so it closes itself.
with open('notes.txt') as f:
for line in f:
print(line.strip())