23
loading...
This website collects cookies to deliver better user experience
if condition:
if-block
marks = input('Enter your score:')
if int(marks) >= 40:
print("You have passed")
Enter your score:46
You have passed
if condition:
if-block;
else:
else-block;
marks = input('Enter your score:')
if int(age) >= 40:
print("You have passed.")
else:
print("You have failed.")
if if-condition:
if-block
elif elif-condition1:
elif-block1
elif elif-condition2:
elif-block2
...
else:
else-block
marks = input('Enter your score:')
your_marks = int(marks)
if your_marks >= 70:
print("Your grade is A")
elif your_marks >= 60:
print("Your grade is B")
else:
print("null")
Enter your score:70
Your grade is A
for index in range(n):
statement
for index in range(5):
print(index)
0
1
2
3
4
range(start,stop)
for index in range(1, 4):
print(index)
1
2
3
4
range(start, stop, step)
for index in range(0, 11, 2):
print(index)
0
2
4
6
8
10
sum = 0
for num in range(51):
sum += num
print(sum)
1275
while condition:
body
max = 5
counter = 0
while counter < max:
print(counter)
counter += 1
0
1
2
3
4
for index in range(0, 11):
print(index)
if index == 3:
break
0
1
2
3
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that are sent to the function when it is called.
From this example;
def addNumbers(a, b):
sum =a + b
print("The sum is " ,sum)
addNumbers(2,5)
def sum(a, b):
return a + b
total = sum(1,20)
print(total)
21
def countdown(n):
print(n)
if n == 0:
return # Terminate recursion
else:
countdown(n - 1) # Recursive call
countdown(5)
5
4
3
2
1
0
def sum(n):
if n > 0:
return n + sum(n - 1)
return 0
result = sum(100)
print(result)
lambda arguments : expression
def times(n):
return lambda x: x * n
double = times(2)
result = double(2)
print(result)
result = double(3)
print(result)
def my_decorator_func(func):
def wrapper_func():
# Do something before the function.
func()
# Do something after the function.
return wrapper_func
@my_decorator_func
def my_func():
pass