1. print():
This command will print a specified message to the console.
print("Hello, world!")
2. input():
This command will prompt the user to enter a value and store it as a string.
name = input("What is your name? ")
3. int(), float(), str():
These commands will convert a value to an integer, float, or string, respectively.
age = int(input("What is your age? "))
price = float(input("What is the price? "))
name = str(input("What is your name? "))
4. if, elif, else:
These commands are used to create conditional statements.
if age < 18:
print("You are a minor.")
elif age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")
5. for loop:
This command will iterate through a sequence of values.
for number in range(1, 11):
print(number)
6. while loop:
This command will execute a block of code repeatedly as long as a certain condition is met.
while age < 18:
print("You are not yet an adult.")
age = age + 1
7. break:
This command will exit a loop early.
while True:
user_input = input("Enter 'q' to quit: ")
if user_input == 'q':
break
8. continue:
This command will skip the rest of the current iteration of a loop and move on to the next one.
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
9. def:
This command is used to define a function.
def greet(name):
print("Hello, " + name + "!")
10. return:
This command is used to specify a value to be returned by a function.
def add(x, y):
return x + y
11. import:
This command is used to import a module or library.
import math
12. len():
This command will return the length of a sequence.
names = ["John", "Jane", "Jim"]
length = len(names)
13. range():
This command will generate a sequence of numbers.
for number in range(1, 11):
print(number)
14. enumerate():
This command will iterate through a sequence and return both the index and the value for each element.
names = ["John", "Jane", "Jim"]
for index, name in enumerate(names):
print(index, name)
15. sorted():
This command will return a sorted list.
numbers = [3, 1, 4, 2]
sorted_numbers = sorted(numbers)
16. zip():
This command will iterate through multiple sequences and combine their elements into tuples.
names = ["John", "Jane", "Jim"]
ages = [20, 30, 40]
for name, age in zip(names, ages):
print(name, age)
17. map():
This command will apply a function to each element in a sequence.
def double(x):
return x * 2
numbers = [1, 2, 3]
doubled_numbers = map(double, numbers)
18. filter():
This command will filter a sequence based on a certain condition.
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5]
even_numbers = filter(is_even, numbers)
19. reduce():
This command will apply a function to a sequence and reduce it to a single value.
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4]
total = reduce(add, numbers)
20. lambda:
This command allows the creation of anonymous functions. This function takes one or more arguments and returns an expression. The arguments and the expression are separated by a colon (:). Here is an example of how you can use a lambda function:
lambda arguments: expression
double = lambda x: x * 2
print(double(5)) # prints 10
In this example, the lambda function takes one argument (x) and returns the value of x multiplied by 2. The function is assigned to the variable “double”, so it can be called a normal function. When the function is called with argument 5, it returns 10.
Lambda functions are often used in combination with other functions such as map(), filter(), and reduce(). They can be useful when you need to define a simple function for a short period of time and don’t want to define a full function using the def keyword.