0% found this document useful (0 votes)
4 views21 pages

Python

notes

Uploaded by

parkarnetraa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Python

notes

Uploaded by

parkarnetraa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

PYTHON CONTEST MASTER SHEET –

DEFINITIONS:-

1. Variable – A named location in memory to store data.


2. Data Type – Classification of data like int, float, str, etc.
3. Mutable – Values can be changed after creation (list, dict, set).
4. Immutable – Values cannot be changed after creation (int, str, tuple).
5. String – Sequence of characters in quotes.
6. List – Ordered, mutable collection.
7. Tuple – Ordered, immutable collection.
8. Dictionary – Unordered key-value pairs.
9. Set – Unordered collection of unique items.
10. Function – Reusable block of code, defined using def.
11. Indentation – Spaces at the start of a line that define code blocks.
12. Loop – Repeats code multiple times.
13. For Loop – Iterates over a sequence.
14. While Loop – Runs while condition is true.
15. Conditional Statement – Runs code if a condition is true (if, elif, else).
16. Operator – Symbol that performs an operation on values.
17. Arithmetic Operators – +, -, \*, /, %, //, \*\*.
18. Comparison Operators – ==, !=, <, >.
19. Logical Operators – and, or, not.
20. Membership Operators – in, not in.
21. Identity Operators – is, is not.
22. Indexing – Accessing elements via position number.
23. Slicing – Extracting part of a sequence (start\:end\:step).
24. Comment – Notes ignored by Python (# or ''' for multi-line).
25. Type Casting – Converting data types (int(), str()).
26. Syntax Error – Mistake in code that breaks Python rules.
27. Runtime Error – Error during execution.
28. Print Function – Displays output.
29. Input Function – Reads user input.
30. Range Function – Creates a sequence of numbers.

1. Python Basics
Case sensitive Name name

Comments # single line / ''' multi line '''

Variables no need to declare type, e.g. x = 5

Keywords if, else, for, while, def, return, True, False, None, and, or, not

2. Data Types
Type Example Mutable?
int 5 No
float 5.2 No
str "hello" No
list [1, 2, 3] Yes
tuple (1, 2, 3) No
dict {"a": 1, "b": 2} Yes
set {1, 2, 3} Yes

3. Operators
Arithmetic: + - * / // % **
Comparison: == != > < >= <=

Logical: and or not

Membership: in, not in

Identity: is, is not

4. Strings
python
Copy
Edit
s = "python"
s[0] # 'p'
s[-1] # 'n'
s[1:4] # 'yth'
len(s) # 6
s.upper() # 'PYTHON'
s.lower() # 'python'

5. Lists
nums = [1, 2, 3]
nums.append(4) # [1, 2, 3, 4]
nums.remove(2) # [1, 3, 4]
nums[1] = 10 # [1, 10, 4]
6. Conditional Statements
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")

7. Loops
for i in range(5): # 0 1 2 3 4
print(i)

while x < 5:
print(x)
x += 1

8. Functions
def add(a, b):
return a + b
print(add(2, 3))
9. Input/Output
python
Copy
Edit
name = input("Enter name: ")
n = int(input("Enter number: "))
10. Common Short Programs
Reverse String s[::-1]

Sum of List sum(lst)

Max of List max(lst)

Check Prime loop till n

Count Vowels check in "aeiou"

11. Common Mistakes in Contests


Forgetting to convert input to int

Index errors (list index out of range)

Missing colon (:) after if, for, def

Case sensitivity errors

________________________________________________________________________________________________

MCQs:-

1. Which of the following is a valid variable name?


A) 2name
B) name_2
C) name-2
D) name 2
Answer: B

2. Output of:
print(type(5/2))
A) <class 'int'>
B) <class 'float'>
C) <class 'double'>
D) Error
Answer: B

3. Output of:
print(3 * "Hi")
Answer: HiHiHi

4. Which is not a data type in Python?


A) list
B) set
C) array
D) tuple
Answer: C (array is in a module, not a built-in type)

5. Output of:
print("5" + "6")
Answer: 56 (string concatenation)

6. What will bool(0) return?


Answer: False
7. Output of:
print(2 ** 3 ** 2)
Answer: 512 (right-to-left exponentiation)

8. Which of these is mutable?


Answer: List

9. What will be printed?


x = [1, 2, 3]
x[1] = 10
print(x)
Answer: [1, 10, 3]

10. Which function is used to find the largest value?


Answer: max()

11. What is the index of the last element in a list of length 5?


Answer: 4

12. Output of:


print("python"[1:4])
Answer: yth (1 to 3)

13. Which keyword is used for a function in Python?


Answer: def

14. Output of:


for i in range(2, 10, 3):
print(i, end=" ")
Answer: 2 5 8

15. What will this print?


print(bool(" "))
Answer: True (non-empty string is True, even if it’s just space)

16. Which function gives the length of a string?


Answer: len()

17. Output of:


a=5
b=2
print(a % b)
Answer: 1 (modulus)

18. What will happen?


x=5
print(x == 5 and x > 2)
Answer: True

19. Which method adds an element to a list?


Answer: .append()

20. Output of:


print("A" in "APPLE")
Answer: True

21. Which statement is correct for comments in Python?


Answer: Start with #

22. Output of:


for i in range(1, 4):
print(i * "*")
Answer:

*
**
***

23. What will this print?


print(5 > 3 or 2 > 10)
Answer: True

24. Which operator is used for floor division?


Answer: //

25. Output of:


print(int("5") + 2)
Answer: 7

26. Which one will cause an error?


A) len("Hi")
B) len(123)
Answer: B (len() works only on sequences, not integers)

27. Output of:


x = [1, 2]
y=x
y.append(3)
print(x)
Answer: [1, 2, 3] (same reference)

28. What is type( (5,) )?


Answer: <class 'tuple'> (single-element tuple needs a comma)

29. Output of:


print("abc" * 0)
Answer: "" (empty string)

30. Which function converts string to lowercase?


Answer: .lower()

Q1. What will this code output?


print(5 // 2)
A) 2.5
B) 2
C) 3
D) 2.0
Answer: B (integer division)

Q2. Which data type is ["apple", "banana"]?


A) Tuple
B) List
C) Dictionary
D) Set
Answer: B
Q3. Output of:
print("python"[::-1])
A) python
B) nohtyp
C) pytho
D) Error
Answer: B (string slicing reverse)

Q4. Which operator is used for exponent in Python?


A) ^
B) **
C) pow
D) ^^
Answer: B

Q5. Which of these is immutable?


A) List
B) Set
C) Tuple
D) Dictionary
Answer: C

Q6. What will this print?


x = 10
x += 5
print(x)
Answer: 15
Q7. Which function returns the length of a list?
A) count()
B) size()
C) len()
D) length()
Answer: C

Q8. Output of:


print(2 ** 3 ** 2)
Answer: 512 (right-to-left exponentiation)

Q9. How do you take integer input?


Answer: int(input())

Q10. Output of:


print(bool(""))
Answer: False (empty string is false)

_______________________________________________________________________________________________

CODING PRACTICE-

1. Reverse a string
s = input()
print(s[::-1])

2. Even or odd
n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd")

3. Factorial
n = int(input())
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)

4. Fibonacci
n = int(input())
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a+b

5. Count vowels
s = input().lower()
count = 0
for ch in s:
if ch in "aeiou":
count += 1
print(count)

6. Largest of three
a, b, c = map(int, input().split())
print(max(a, b, c))

7. Sum of list
nums = list(map(int, input().split()))
print(sum(nums))

8. Palindrome check
s = input()
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

9. Multiplication table
n = int(input())
for i in range(1, 11):
print(n, "x", i, "=", n*i)

10. Prime check


n = int(input())
if n > 1:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

1. Largest of Two Numbers


a, b = map(int, input().split())
print(max(a, b))

2. Largest of Three Numbers


a, b, c = map(int, input().split())
print(max(a, b, c))

3. Factorial of a Number
n = int(input())
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)

4. Prime Numbers up to N
n = int(input())
for i in range(2, n+1):
for j in range(2, i):
if i % j == 0:
break
else:
print(i, end=" ")

5. Armstrong Number
n = int(input())
p = len(str(n))
print("Armstrong" if sum(int(d)**p for d in str(n)) == n else "Not Armstrong")

6. Remove Duplicates from List


lst = list(map(int, input().split()))
print(list(set(lst)))

7. Sort List
lst = list(map(int, input().split()))
print(sorted(lst))
8. Find Second Largest
lst = list(map(int, input().split()))
print(sorted(set(lst))[-2])

9. Table of a Number
n = int(input())
for i in range(1, 11):
print(n, "x", i, "=", n*i)

10. GCD (HCF) of Two Numbers


import math
a, b = map(int, input().split())
print(math.gcd(a, b))

11. LCM of Two Numbers


import math
a, b = map(int, input().split())
print(math.lcm(a, b))
12. Leap Year Check
y = int(input())
print("Leap" if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0) else "Not Leap")
13. Swap Two Numbers (Without Temp)
a, b = map(int, input().split())
a, b = b, a
print(a, b)

14. List Comprehension (Squares)


n = int(input())
print([i**2 for i in range(1, n+1)])
s = input()
print(len(s.split()))

16. Character Frequency


s = input()
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
print(freq)

17. Simple Calculator


a, b = map(float, input().split())
op = input()
if op == '+': print(a+b)
elif op == '-': print(a-b)
elif op == '*': print(a*b)
elif op == '/': print(a/b if b != 0 else "Error")
else: print("Invalid")
______________________________________________________________________________________________

EXTRASSSSSSSSSS-

1. Python Basics — Key Definitions

Variable: A name given to a value stored in memory.


Data Types:

* int whole numbers


* float decimal numbers
* str text (string)
* bool True/False
* list ordered, changeable
* tuple ordered, unchangeable
* set unordered, unique items
* dict key–value pairs

Type Casting: Converting data type int(), float(), str()

Operators:

* Arithmetic: + - \* / // % \*\*
* Comparison: == != > < >= <=
* Logical: and or not

Control Flow:
* if, elif, else for decisions
* for and while loops for repetition

Functions: Blocks of code run when called, defined using def.

Mutable vs Immutable: Lists, sets, dicts are mutable; strings, tuples are immutable.

Indexing & Slicing: Access parts of a sequence using \[] and \[:].

Modules: Pre-written code you can import import math

2. MCQ Focus Areas

* Predict output from small code snippets.


* Identify errors (indentation, wrong syntax).
* Data type conversions.
* Operator precedence:
Highest () \*\* \* / // % + - comparison not and or

String slicing examples:


s = "Python"
s\[1:4] # "yth"
s\[::-1] # reverse

is vs ==:

* is checks object identity


* \== checks value equality
Mutable vs Immutable behavior in assignments.

3. Likely Coding Questions for Round 2

Practice & Memorize:

1. Reverse a string
2. Check palindrome (string/number)
3. Find factorial of a number
4. Prime number check / list primes in range
5. Fibonacci series
6. Armstrong number check
7. Count vowels in a string
8. Find largest/smallest in list
9. Sum of list elements
10. Sort list without using sort() (bubble sort logic)

4. Speed & Accuracy Tips

* Use map(int, input().split()) for multiple integer inputs in one line.


* Always test with small and edge cases (0, 1, negatives).
* If stuck, break problem into input processing output.
* For strings, remember .lower() and .upper() for case handling.
* Use f"" (f-strings) for clean, fast printing.
5. Contest Survival Strategy

Round 1 (MCQ):

* Read last line first know the final output requirement.


* Watch for tricky options like '5' + 2 (error) vs '5' \* 2 ('55').

Round 2 (Coding):

* Write the logic first, then handle inputs/outputs as per question format.
* Keep your code simple — don’t overcomplicate.
* If possible, finish early & test with given + custom test cases.

6. Common Pitfalls to Avoid

* Forgetting int() after input() for math.


* Writing if a = b: instead of if a == b:.
* Missing colons : after if, for, while, def.
* Infinite while loops.
* Wrong indentation — Python is sensitive!

You might also like