Its features make it an excellent choice for beginners
When someone says "I want a programming language in which I need only say what I wish done" give him a lollipop.
Python code can be executed in a shell by typing python $FILENAME
#!/usr/bin/env python3
"""
A 'Hello world!' program written in Python.
"""
print('Hello, World!')
Download hello.py
Python offers it's own read–eval–print loop (REPL),
the Python shell
Python commands are immediately executed
python
or ipython
as a calculator(i)python
When encountering an error message you do not understand, try pasting it into your favorite search engine
Allows to execute different commands based on provided conditions
if <assignment_expression>:
<suite>
(elif <assignment_expression>: <suite>)*
[else: <suite>]
if
)#!/usr/bin/env python3
LANG = 'en'
# ...
if LANG == 'en':
output = 'Good Morning!'
elif LANG == 'de':
output = 'Guten Morgen!'
else:
output = 'Bonjour!'
print(output)
<expression1> if <condition> else <expression2>
#!/usr/bin/env python3
LANG = 'en'
# ...
output = "Good Morning!" if LANG == 'en' else "Guten Morgen!"
print(output)
Allow splitting up error detection and error handling
try:
<suite>
(except [<expression> [as <identifier>]]: <suite>)+
[else: <suite>]
[finally: <suite>]
try
)#!/usr/bin/env python3
length = 3
rectangle_area = 12
# ...
try:
width = rectangle_area / length
except ZeroDivisionError:
print("The length of a rectangle must not be 0")
# deal with problem or re-raise the exception
raise
print(width)
while
Statement...is used for repeated execution as long as an expression is true
while <assignment_expression>:
<suite> # may include `break` and `continue`
[else: <suite>]
#!/usr/bin/env python3
amount = 3000 # initial investment in euro
GOAL = 4000
INTEREST = 0.03 # 3%
years = 0
while amount < GOAL:
years += 1
amount = amount * (1 + INTEREST)
print(f"you'll reach your investment goal after {years} years")
if/ else
brancheswhile
loopsprint(.)
, input(.)
,
int(.)
random.randint(.)