Python Contest Guide
Python Contest Guide
1. Input/Output
Basic input:
name = input()
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)
List:
arr = [1, 2, 3]
arr.append(4)
arr.sort()
String:
s = 'hello'
print(s.upper(), s[::-1])
5. Functions
return a + b
print(add(2, 3))
Example:
nums = [4, 2, 8]
print(max(nums), sum(nums))
nums = [5, 2, 1]
nums.sort()
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))
t = int(input())
for _ in range(t):
n = int(input())
# solve here