0% found this document useful (0 votes)
7 views

python guide 11 to 12 pdf

The document provides a comprehensive guide to problem-solving steps, algorithm representation, and Python programming basics. It covers topics such as data types, control flow, and various programming constructs, along with practical examples and exercises. Additionally, it includes practice questions to reinforce learning in Python programming.

Uploaded by

anuchaitanya2017
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

python guide 11 to 12 pdf

The document provides a comprehensive guide to problem-solving steps, algorithm representation, and Python programming basics. It covers topics such as data types, control flow, and various programming constructs, along with practical examples and exercises. Additionally, it includes practice questions to reinforce learning in Python programming.

Uploaded by

anuchaitanya2017
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

✅ 1.

Introduction to Problem-solving

Steps for Problem-solving:

1. Analyzing the problem: Understand the problem by identifying inputs, outputs, and
requirements.
2. Developing an algorithm: Write step-by-step instructions to solve the problem.
3. Coding: Convert the algorithm into a programming language (Python).
4. Testing: Run the program to verify it produces the expected output.
5. Debugging: Identify and x errors.
Example: Problem: Find the square of a number.

• Analysis: Input → A number. Output → Square of the number.


• Algorithm:
1. Take a number as input.
2. Calculate its square.
3. Display the result.
• Code:
python
num = int(input("Enter a number: "))
square = num ** 2
print("Square:", square)

✅ 2. Representation of Algorithms Using Flowchart and Pseudocode

• Flowchart: A graphical representation of the steps in a process.


• Pseudocode: A simpli ed way of writing the steps in plain English.
Flowchart Symbols:

• Oval: Start/End.
• Rectangle: Process.
• Parallelogram: Input/Output.
• Diamond: Decision-making.
Flowchart for Squaring a Number:

mathematica
┌────────────┐
│ Start │
└────┬───────┘

┌────▼───────┐
│ Input num │
└────┬───────┘

┌────▼─────────────┐
│ square = num ** 2 │
└────┬─────────────┘

1 of 17
fi
fi
┌────▼──────────────┐
│ Print square │
└────┬──────────────┘

┌────▼───────┐
│ End │
└────────────┘

✅ 3. Familiarization with Python Programming

• Python: A high-level, interpreted programming language used for various applications.


• Features of Python:
◦ Easy to learn.
◦ Interpreted language.
◦ Dynamic typing.
◦ Extensive libraries.
Executing a "Hello, World!" Program:

python
print("Hello, World!")
• Execution Modes:
◦ Interactive Mode: Type commands directly into the Python shell.
◦ Script Mode: Save the code in a le (e.g., program.py) and run it using the
terminal.

✅ 4. Python Character Set and Tokens

• Character Set: A set of valid characters (letters, digits, symbols) used in Python programs.

• Tokens:

◦ Keywords: Reserved words (e.g., if, else, for, while).


◦ Identi ers: Variable names.
◦ Literals: Fixed values (e.g., 10, "Hello").
◦ Operators: Symbols for operations (e.g., +, -, *).
◦ Punctuators: Special characters (e.g., :, ,, ()).
Example:

python
# Tokens example
x = 5 # Identifier
print(x + 2) # Operator and literal

✅ 5. Variables, l-value, and r-value

• Variables: Containers for storing data.


• l-value: Represents the memory location where data is stored (left side of =).

2 of 17
fi
fi
• r-value: The actual value being assigned (right side of =).
Example:

python
x = 10 # x is the l-value, 10 is the r-value

✅ 6. Data Types

• Number: int, float, complex


• Boolean: True or False
• Sequence: str, list, tuple
• None: Special data type representing a null value.
• Mapping: dict (key-value pairs)
Example:

python
x = 10 # int
y = 3.14 # float
z = 3 + 4j # complex
flag = True # boolean
text = "Python" # string

✅ 7. Operators

• Arithmetic: +, -, *, /, //, %, **
• Relational: ==, !=, <, >, <=, >=
• Logical: and, or, not
• Assignment: =, +=, -=, *=, /=
• Identity: is, is not
• Membership: in, not in
Example:

python
x = 10
y = 5
print(x + y) # Arithmetic
print(x > y) # Relational
print(x > 5 and y < 10) # Logical

✅ 8. Expressions, Type Conversion, and I/O

• Expression: A combination of variables, operators, and values.


• Type Conversion:
◦ Implicit: Python automatically converts types.
◦ Explicit: You convert types using int(), float(), str().
Example:

3 of 17
python
# Type Conversion
x = "10"
y = int(x) # Explicit conversion
print(type(y)) # <class 'int'>

# Input and Output


name = input("Enter your name: ")
print("Hello", name)

✅ 9. Flow of Control

• Sequential Flow: Code executes line by line.


• Conditional Flow: Decision-making (if, else).
• Iterative Flow: Looping statements (for, while).

✅ 10. Conditional Statements

• if, if-else, if-elif-else


Example:

python
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

✅ 11. Iterative Statements

• For Loop: Used for iteration over a sequence.


• While Loop: Used when the number of iterations is unknown.
Example:

python
# For Loop
for i in range(5):
print(i)

# While Loop
x = 1
while x <= 5:
print(x)
x += 1

4 of 17
✅ 12. Strings

• Operations: Concatenation, repetition, membership, slicing.


• Methods: len(), upper(), lower(), find(), etc.
Example:

python
text = "Python"
print(len(text)) # Length
print(text.upper()) # Uppercase
print(text[0:3]) # Slicing

✅ 13. Lists

• Operations: Indexing, concatenation, repetition.


• Methods: append(), extend(), pop(), sort(), etc.
Example:

python
nums = [1, 2, 3, 4, 5]
nums.append(6)
print(nums)

✅ 14. Tuples

• Immutable sequences.
• Methods: count(), index(), min(), max(), etc.
Example:

python
tpl = (1, 2, 3, 4)
print(tpl[0]) # Indexing

✅ 15. Dictionary

• Key-Value pairs.
• Methods: keys(), values(), items(), get(), update()
Example:

python
person = {"name": "John", "age": 25}
print(person["name"])

✅ 16. Python Modules

5 of 17
• math: sqrt(), ceil(), floor()
• random: randint(), random()
• statistics: mean(), median()
Example:

python
import math
print(math.sqrt(16)) # 4.0

6 of 17
QUESTION AND PRACTICE:—

💡 1. Python Basics and Control Flow

✅ Program 1: Calculate the Area of a Rectangle

python
# Input length and breadth
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))

# Calculate area
area = length * breadth

# Display the result


print("Area of the rectangle:", area)
✅ Program 2: Check if a Number is Even or Odd

python
num = int(input("Enter a number: "))

if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
✅ Program 3: Find the Largest of Three Numbers

python
# Input three numbers
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

# Compare and find the largest


if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
✅ Program 4: Swap Two Numbers

python
x = int(input("Enter value of x: "))
y = int(input("Enter value of y: "))
7 of 17
# Swap values
x, y = y, x

print("After swapping:")
print("x =", x)
print("y =", y)

🔥 2. Looping and Iterative Statements

✅ Program 5: Print Multiplication Table

python
num = int(input("Enter a number: "))

# Using for loop


print("Multiplication Table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
✅ Program 6: Sum of Natural Numbers

python
n = int(input("Enter a positive integer: "))
sum = 0

# Using while loop


i = 1
while i <= n:
sum += i
i += 1

print("Sum of first", n, "natural numbers:", sum)


✅ Program 7: Find Factorial of a Number

python
num = int(input("Enter a number: "))
factorial = 1

for i in range(1, num + 1):


factorial *= i

print("Factorial of", num, "is", factorial)


✅ Program 8: Fibonacci Series

python
8 of 17
n = int(input("Enter the number of terms: "))
a, b = 0, 1

print("Fibonacci Sequence:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
✅ Program 9: Check if a Number is Prime

python

num = int(input("Enter a number: "))


is_prime = True

if num > 1:
for i in range(2, num):
if num % i == 0:
is_prime = False
break

if is_prime:
print(num, "is a Prime number")
else:
print(num, "is not a Prime number")
else:
print(num, "is not a Prime number")
✅ Program 10: Print a Pattern

python
rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):


print("* " * i)

📚 3. Strings and Operations

✅ Program 11: Reverse a String

python
text = input("Enter a string: ")
reverse = text[::-1]
print("Reversed string:", reverse)
✅ Program 12: Count Vowels in a String

python
9 of 17
text = input("Enter a string: ").lower()
vowels = "aeiou"
count = 0

for char in text:


if char in vowels:
count += 1

print("Number of vowels:", count)


✅ Program 13: Check for Palindrome

python
text = input("Enter a string: ").lower()
reverse = text[::-1]

if text == reverse:
print("Palindrome")
else:
print("Not a Palindrome")
✅ Program 14: String Operations

python
text = "Python Programming"
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
print("Length:", len(text))
print("First 6 characters:", text[:6])

🔥 4. Lists and Tuples

✅ Program 15: Find the Largest and Smallest in a List

python
nums = [5, 10, 15, 20, 25]

print("Maximum:", max(nums))
print("Minimum:", min(nums))
✅ Program 16: Sum of List Elements

python
nums = [2, 4, 6, 8, 10]

# Using sum() function


print("Sum of elements:", sum(nums))

10 of 17
✅ Program 17: Remove Duplicates from a List

python
nums = [1, 2, 2, 3, 4, 4, 5]
unique_nums = list(set(nums))

print("List without duplicates:", unique_nums)


✅ Program 18: Tuple Operations

python
# Creating a tuple
tpl = (10, 20, 30, 40)

print("Tuple:", tpl)
print("First element:", tpl[0])
print("Length:", len(tpl))
print("Sum:", sum(tpl))

💡 5. Dictionary Operations

✅ Program 19: Create and Display a Dictionary

python
# Creating a dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}

# Displaying dictionary
print("Dictionary:", person)
print("Name:", person["name"])
print("Age:", person["age"])
✅ Program 20: Count Frequency of Elements in a List

python
nums = [1, 2, 2, 3, 4, 4, 4, 5]
freq = {}

for num in nums:


if num in freq:
freq[num] += 1
else:
freq[num] = 1
11 of 17
print("Frequency:", freq)
✅ Program 21: Merge Two Dictionaries

python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

# Merging dictionaries
merged_dict = {**dict1, **dict2}
print("Merged Dictionary:", merged_dict)

⚙ 6. Python Modules

✅ Program 22: Using the math Module

python
import math

num = 16
print("Square Root:", math.sqrt(num))
print("Power:", math.pow(2, 3))
print("Ceil:", math.ceil(3.2))
print("Floor:", math.floor(3.8))
✅ Program 23: Random Number Generation

python
import random

print("Random Number (1-100):", random.randint(1, 100))


✅ Program 24: Find Mean, Median, and Mode

python
import statistics

data = [10, 20, 30, 40, 50, 50]

print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
print("Mode:", statistics.mode(data))

12 of 17
🔥 📄 Practice Questions – Topic-wise

✅ 1. Problem-solving and Algorithm

1. De ne the steps involved in problem-solving with examples.


2. Write an algorithm to check whether a given number is prime.
3. Create a owchart to nd the sum of digits of a number.
4. Write pseudocode to calculate the area and circumference of a circle.

✅ 2. Python Basics and Tokens

1. What are Python tokens? Explain with examples.


2. Differentiate between interactive mode and script mode in Python.
3. Identify the tokens in the following code:
python
x = 10
if x > 5:
print("Greater")
else:
print("Smaller")
4. Write a program to accept two numbers from the user and display their sum.

✅ 3. Flow of Control

1. Explain the difference between if, if-else, and if-elif-else statements.


2. Write a Python program to check if a number is positive, negative, or zero.
3. Create a owchart for nding the largest of three numbers.
4. Write a program to check if a year is a leap year.

✅ 4. Iterative Statements

1. Explain the difference between for loop and while loop with examples.
2. Write a program to print the multiplication table of a given number.
3. Write a program to calculate the sum of the rst n natural numbers.
4. Create a pattern printing program using nested loops:
markdown
*
* *
* * *
* * * *

✅ 5. Strings

1. Write a program to count the number of vowels in a string.


2. Write a program to reverse a string using slicing.
3. Differentiate between immutable and mutable data types with string examples.
13 of 17
fi
fl
fl
fi
fi
fi
4. Write a program to check whether a given string is a palindrome.

✅ 6. Lists

1. Write a program to nd the maximum and minimum elements in a list.


2. Create a program to remove duplicates from a list.
3. Write a program to sort a list of numbers in ascending order.
4. Create a program to nd the frequency of elements in a list using a dictionary.

✅ 7. Tuples

1. Explain the difference between lists and tuples.


2. Write a program to nd the sum of tuple elements.
3. Create a program to merge two tuples.
4. Write a program to nd the index of an element in a tuple.

✅ 8. Dictionaries

1. Write a program to create a dictionary of employee names and salaries.


2. Write a program to merge two dictionaries.
3. Create a program to count the frequency of characters in a string using a dictionary.
4. Write a program to delete an item from a dictionary by key.

✅ 9. Python Modules

1. Write a program using the math module to calculate the square root and power of a
number.
2. Use the random module to generate a random number between 1 and 100.
3. Write a program using the statistics module to nd the mean, median, and mode of
a list of numbers.
4. Simulate the rolling of two dice using the random module.

🔥 📄 Sample Paper – Computational Thinking and Programming - I

SECTION A: Multiple Choice Questions (1 mark each)

1. Which of the following is an immutable data type in Python?


a) List
b) Dictionary
c) Tuple
d) Set

2. What does the following expression evaluate to?

14 of 17
fi
fi
fi
fi
fi
python
print(5 // 2)
a) 2.5
b) 2
c) 3
d) 1

3. Identify the membership operator:


a) ==
b) in
c) !=
d) and

4. What will be the output of the following code?

python
x = 10
y = 5
print(x > y and x < 15)
a) True
b) False
c) Error
d) None

🔥 1. Flowchart for Finding the Largest of Three Numbers

I'll generate a owchart showing the logic to nd the largest of three numbers.

🔹 Algorithm:

1. Take three numbers as input: a, b, and c.


2. Compare:
◦ If a > b and a > c: a is the largest.
◦ Else if b > a and b > c: b is the largest.
◦ Else: c is the largest.
3. Display the largest number.

🔹 Flowchart:

15 of 17
fl
fi
🔥 2. Flowchart for Calculating the Factorial of a Number

🔹 Algorithm:

1. Take an integer n as input.


2. Initialize fact = 1.
3. Use a for loop:
◦ Multiply fact by each value from 1 to n.
4. Print the result.
🔹 Flowchart:

🔥 3. Flowchart for Checking Whether a Number is Prime

🔹 Algorithm:

1. Take an integer n as input.


2. Check if n is less than or equal to 1 → Not prime.
3. Use a loop to divide n by numbers from 2 to n-1:
◦ If n is divisible by any number in this range → Not prime.
◦ If no divisors are found → Prime.
4. Print the result.

🔹 Flowchart:
16 of 17
🔥 4. Flowchart for Fibonacci Series

🔹 Algorithm:

1. Take the number of terms n as input.


2. Initialize a = 0, b = 1.
3. Print the rst two terms.
4. Use a loop:
◦ Calculate the next term by adding the previous two terms: next = a + b.
◦ Update a and b.
◦ Repeat until n terms are printed.
🔹 Flowchart:

17 of 17
fi

You might also like