Python Syntax, Functions, Lambda, Lists, Input, and Loops
1. Python Functions
-------------------
Syntax:
def function_name(parameters):
Function docstring
statement(s)
return value
Example:
def greet(name):
return "Hello, " + name
print(greet("Alice")) # Output: Hello, Alice
2. Lambda Functions
-------------------
Syntax:
lambda arguments: expression
Example:
square = lambda x: x * x
print(square(5)) # Output: 25
add = lambda x, y: x + y
print(add(3, 4)) # Output: 7
3. Lists
--------
3.1 1D List:
lst = [1, 2, 3, 4]
print(lst[0]) # Output: 1
3.2 2D List:
matrix = [[1, 2], [3, 4]]
print(matrix[1][0]) # Output: 3
3.3 3D List:
cube = [[[1], [2]], [[3], [4]]]
print(cube[1][0][0]) # Output: 3
3.4 Complex Mixed List:
mixed = [1, [2, 3], [[4, 5], 6]]
print(mixed[2][0][1]) # Output: 5
4. Input
--------
# name = input("Enter your name: ")
# print("Hello", name)
# number = int(input("Enter a number: "))
# print("Square is", number * number)
5. Loops
--------
5.1 For Loop:
for i in range(5):
print(i) # Outputs 0 to 4
5.2 While Loop:
i=0
while i < 5:
print(i)
i += 1
5.3 Nested Loops:
for i in range(2):
for j in range(2):
print(f"i={i}, j={j}")
Conclusion:
This document gives an overview of Python's essential syntax for functions, lambda, lists of various types,