Python

Python is a high-level, interpreted programming language known for its simplicity and readability. It is widely used for web development, automation, data analysis, artificial intelligence, and more. Python supports multiple programming paradigms and has a rich standard library.

Introduction to Python

Python code is written in .py files. It uses indentation to define blocks instead of braces or keywords. Python programs can be run using an interpreter from the command line or an IDE.

Variables and Data Types

Python has dynamic typing. Variables can store values of different types: integers, floats, strings, lists, tuples, dictionaries, and booleans.

x = 10
y = 5.5
name = "Alice"
is_active = True

Operators

Python supports arithmetic, comparison, logical, assignment, and bitwise operators.

sum = x + y
diff = x - y
product = x * y
quotient = x / y
is_equal = x == 10

Conditional Statements

Python uses if, elif, and else for decision making.

if x > y:
  print("x is greater than y")
elif x == y:
  print("x is equal to y")
else:
  print("x is less than y")

Loops

Python supports for and while loops for iteration.

for i in range(5):
  print(i)

count = 0
while count < 5:
  print(count)
  count += 1

Functions

Functions in Python are defined using def keyword.

def greet(name):
  print("Hello, " + name)

greet("Alice")

collatz_example.py

n = 12 # Starting number
print("Collatz sequence for", n)
while n != 1:
  if n % 2 == 0:
    n = n // 2
  else:
    n = 3 * n + 1
  print(n)

MCQs

1. Which symbol is used for comments in Python?

(a) //

(b) #

(c) <!-- -->

(d) /**/

► (b) #

2. How do you define a function in Python?

(a) func myFunc():

(b) def myFunc():

(c) function myFunc():

(d) define myFunc():

► (b) def myFunc():

3. Which of these is a Python data type?

(a) int

(b) float

(c) list

(d) All of the above

► (d) All of the above

4. What is the output of print(2 ** 3)?

(a) 6

(b) 8

(c) 9

(d) 23

► (b) 8

5. How do you start a while loop?

(a) while condition:

(b) loop while condition

(c) do while condition

(d) while (condition)

► (a) while condition:

6. Which keyword is used for conditional statements?

(a) if

(b) select

(c) switch

(d) case

► (a) if

7. Which symbol is used for multiplication?

(a) x

(b) *

(c) &

(d) %

► (b) *

8. How do you create a list in Python?

(a) []

(b) ()

(c) {}

(d) <>

► (a) []

9. How do you print output to the screen?

(a) echo()

(b) print()

(c) display()

(d) console.log()

► (b) print()

10. Which operator is used for exponentiation?

(a) ^

(b) **

(c) pow()

(d) exp()

► (b) **

11. How do you check equality in Python?

(a) =

(b) ==

(c) !=

(d) >

► (b) ==

12. Which of these is a boolean value?

(a) True

(b) False

(c) None

(d) Both (a) and (b)

► (d) Both (a) and (b)

13. Which function returns the length of a list?

(a) size()

(b) length()

(c) len()

(d) count()

► (c) len()

14. Which keyword is used to exit a loop?

(a) exit

(b) stop

(c) break

(d) end

► (c) break

15. How do you create a dictionary?

(a) []

(b) ()

(c) {}

(d) <>

► (c) {}

16. How do you import a module in Python?

(a) include

(b) import

(c) require

(d) load

► (b) import

17. Which keyword is used to create a class?

(a) class

(b) object

(c) def

(d) type

► (a) class

18. What is the output of print(type(5))?

(a) int

(b) 'int'

(c) <class 'int'>

(d) number

► (c) <class 'int'>