There is nearly every problem already solved and every question already asked!
def main():
print('Hello, world!')
main()
sum = a + b + c
sum = (
a
+ b
+ c
)
def abs_diff(a, b):
if a > b:
return a - b
return b - a
#
c = a + b # I'm a comment
# There are
# no block
# comments
=, no keyword needed.my_variable = 1 # valid
π = 3.14 # valid
2_chars = 'ab' # not valid
a, b = 1, 2
| Type | Description | Example |
|---|---|---|
| NoneType | Not a value | None |
| bool | Boolean | True, False |
| int | Integer | 10 |
| float | Floating Point number | 10.0 |
| complex | Complex number | 1+1j |
| object | Object | object() |
| Type | Description | Example |
|---|---|---|
| str | String | 'Hi!' |
| tuple | Unmutable list | (1, 2) |
| list | Mutable list | [1, 2] |
| set | Set | {1, 2} |
| dict | Dictionary | {1: 2} |
' or with "
a = 'Hello'
b = "World"
a = "'Hello'"
b = '"World"'
a = """I am a string
that spans over
multiple lines
"""
[1, 'a', None]
(1, [2, 3])
list((1, 2))
tuple([1, 2])
{} ← this is an empty dict!
set() # this an empty set
{1: {1, 2}}
{1: 1, '2': '2', 3.0: 3.0}
hash(dict())
[]'Hello'[0]
from:to (to is not included)
'Hello'[1:3]
'Hello'[2:]
'Hello'[:2]
'Hello'[-2:]
'Hello'[:-2]
[]
a = {1: 'a'}
a[1] # 'a'
a = {1, 2}
tuple(a)[0]
| Operator | Description | Example |
|---|---|---|
| and | logical AND | True and False # False |
| or | logical OR | True or False # True |
| not | logical NOT | not True # False |
| ^ | logical XOR | True ^ True # False |
not? (5')| Operator | Description | Example |
|---|---|---|
| + | Addition / Add strings | 1 + 2.0 # 2.0 |
| - | Subtraction | 1 - 2.0 # -1.0 |
| * | Multiplication | 1 * 2.0 # 2.0 |
| / | Division | 1 / 2 # 0.5 |
| // | Integer division | 1 // 2 # 0 |
| % | Modulo | 1 % 2 # 1 |
| ** | Power | 2 ** 3 # 8 |
| Operator | Description | Example |
|---|---|---|
| is | Equal (bool, None) | None is True # False |
| == | Equal (everything else) | 1 == 2 # False |
| is not | Not equal (bool, None) | False is not None # True |
| != | Not equal (everything else) | 1 != 2 # True |
| < | Less than | 1 < 2 # True |
| ≤ | Less or equal than | 1 <= 2 # True |
| > | Greater than | 2 > 3 # False |
| ≥ | Greater or equal than | 2 >= 3 # False |
| in | Contains | 1 in [] # False |
if: conditional blockelif: additional conditional block(s)else: if no condition metif x > 0:
do_somethig()
elif x < -10:
do_something_else()
elif x < 0:
do_yet_something_else()
else:
fall_back()
while: run while the condition is metbreak: stop the while loopcontinue: skip the rest of blockelse: in case no break occured
while x > a:
x = x - b
if x < a:
break
if x < c:
continue
x = x - d
else:
print('no break!')
for: iterate over all itemsbreak: stop the while loopcontinue: skip rest of blockelse: in case no break occured
for x in (1, 2, 3, 4):
if x > 2:
continue
if x < 3:
break
else:
print('no break!')
def keywordreturn (something). If not, None is returned=)def sum(a, b, c=0, d=0):
return a + b + c + d
sum(1, 2)
sum(1, 2, 3)
sum(a=1, b=2, c=3)
sum(1, 2, d=4)
How can you print a countdown (10 to 0)?
from ... import ...
from datetime import date, datetime
{}
name = 'World'
f'Hello, {name}' # 'Hello, World'
result = 1.23345
f'The result is {result:.2f}' # 'The result is 1.23'
a = 5
b = 6
f'{a} * {b} is {a*b}' # '5 * 6 is 30'
Create a random password generator. The password has to have a length of at least 8 characters, at least 1 digit and 1 special char (_ or -).
Validate a telephone number, as if written on an input form. Telephone numbers can be written as ten digits, or with dashes, spaces, or dots between the three segments, or with the area code parenthesized; both the area code and any white space between segments are optional.
Create a program that prompts for a quote and an author. Display the quotation and author as shown here:
[Author] says, "[Quote]" (Replace [Author] and [Quote] with the actual values.
You might want to use quotes.rest and requests.
Create a program that prompts for ingredients. Display possible food recipes.