Python Programming Contest Guide (Beginner to Lower-Intermediate)
1. Input/Output
Basic input:
name = input()
Input with prompt:
age = int(input('Enter age: '))
Multiple inputs:
a, b = map(int, input().split())
Output:
print('Hello')
print(f'Value is {a}')
2. Conditional Statements
if x > 0:
print('Positive')
elif x == 0:
print('Zero')
else:
print('Negative')
3. Loops
for i in range(5):
print(i)
while x > 0:
x -= 1
print(x)
Python Programming Contest Guide (Beginner to Lower-Intermediate)
4. List & String Manipulation
List:
arr = [1, 2, 3]
arr.append(4)
arr.sort()
String:
s = 'hello'
print(s.upper(), s[::-1])
5. Functions
def add(a, b):
return a + b
print(add(2, 3))
6. Useful Built-in Functions
len(), range(), max(), min(), sum(), sorted()
Example:
nums = [4, 2, 8]
print(max(nums), sum(nums))
7. Sorting & Searching
nums = [5, 2, 1]
nums.sort()
Binary Search (from bisect module):
from bisect import bisect_left
Python Programming Contest Guide (Beginner to Lower-Intermediate)
8. Dictionaries & Sets
d = {'a': 1, 'b': 2}
print(d['a'])
s = set([1,2,3])
s.add(4)
9. Recursion Example
def fact(n):
if n == 0: return 1
return n * fact(n-1)
print(fact(5))
10. Problem Solving Template
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
# solve here