Python Semester 1 Study Guide 2024-2025 PDF
Document Details
![IdealString7596](https://quizgecko.com/images/avatars/avatar-10.webp)
Uploaded by IdealString7596
North American International School
2024
Ms. Zohra Kashif
Tags
Summary
This is a study guide for Python Semester 1, covering topics such as programming basics and essential programming concepts. It provides examples, pseudocode, and practice questions, likely intended for use at North American International School.
Full Transcript
Python Semester 1 Study Guide Ms. Zohra Kashif 2024-2025 Section A- What You Need to Know Section B- Practice to Prepare Section A The following material includes material that has been taught in semester 1 and will be assessed. Standards Addressed C...
Python Semester 1 Study Guide Ms. Zohra Kashif 2024-2025 Section A- What You Need to Know Section B- Practice to Prepare Section A The following material includes material that has been taught in semester 1 and will be assessed. Standards Addressed California Standards Alignment This guide aligns with California K-12 Computer Science Standards, particularly those focusing on computational thinking, problem-solving, and programming skills. Standards Covered: 3A-AP-13-Create prototypes that use algorithms to solve computational problems by leveraging prior student knowledge and personal interests. 3A-AP-14-Use lists to simplify solutions, generalizing computational problems instead of repeatedly using simple variables. 3A-AP-16-Design and iteratively develop computational artifacts for practical intent, personal expression, or to address a societal issue by using events to initiate instructions. 3A-DA-09-Translate between different bit representations of real-world phenomena, such as characters, numbers, and images. 3A-AP-16-Design and iteratively develop computational artifacts for practical intent, personal expression, or to address a societal issue by using events to initiate instructions. 3A-AP-17-Decompose problems into smaller components through systematic analysis, using constructs such as procedures, modules, and/or objects. 3A-AP-18-Create artifacts by using procedures within a program, combinations of data and procedures, or independent but interrelated programs. 3A-AP-20-Evaluate licenses that limit or restrict use of computational artifacts when using resources such as libraries. Table of Contents 1. Programming Basics 2. Pseudocode 3. Variables and Data Types 4. Operators and Expressions 5. String Formatting 6. Conditional Statements 7. Loops 8. Functions 9. Nested Loops 10. Debugging Programs 11. Dictionaries 12. List of tuples 13. Control flow 14. Vocabulary 15. Useful Links Programming Basics Definition: Programming is the process of creating a set of instructions for a computer to perform specific tasks. 1. Basic Concepts Definitions and Explanations a) Programming Programming is the process of writing instructions that a computer can execute to perform specific tasks. Core Python Concepts a) Objects In Python, everything is an object. Objects have attributes (data) and methods (functions that act on data). Example: x = 10 # 'x' is an object of type 'int' print(type(x)) Pseudocode Definition: Pseudocode is a simplified, human-readable version of programming code that uses plain language to describe the logic of an algorithm Pseudocode is a plain language description of the steps in an algorithm, used for planning code. Example: BEGIN INPUT number1, number2 sum = number1 + number2 PRINT sum END Pseudocode Definition: Pseudocode is a plain language description of the steps in an algorithm, used for planning code. Example: BEGIN INPUT number1, number2 sum = number1 + number2 PRINT sum END oat name = "Alice" # string is_student = True # boolean Variables and Data Types Variables: Store data values. Common Data Types: int (integer) float (decimal) str (string) bool (boolean) Example: age = 15 # integer price = 19.99 # fl1. Programming Basics b) Variables A variable stores data that can be used and manipulated. Example: Definition: Programming is the process of creating a set of instructions for a computer to perform specific tasks. Pseudocode Definition: Pseudocode is a plain language description of the steps in an algorithm, used for planning code. Example: BEGIN INPUT number1, number2 sum = number1 + number2 PRINT sum END oat name = "Alice" # string is_student = True # boolean Practice Question: Q: What will the following code output? x = 10 y = 20 print(x + y) A: 30 Operators and Expressions Operators are symbols used to perform operations: Arithmetic Operators: +, -, *, / Comparison Operators: ==, !=, >, < Logical Operators: and, or, not Practice Question: Q: What is the output of: print(5 > 3 and 4 < 10) A: True 5. String Formatting String formatting allows embedding variables within strings. Example: name = "John" age = 25 print(f"My name is {name} and I am {age} years old.") Output: My name is John and I am 25 years old. 6. Conditional Statements Conditional statements control the flow of a program based on conditions. Example: if score >= 90: print("A") elif score >= 80: print("B") else: print("C") 7. Loops Loops are used to repeat actions. o for loop o while loop Example (for loop): for i in range(5): print(i) Output: 0 1 2 3 4 Practice Question: Q: What is the output of the following code? count = 1 while count 10: print("x is greater than 10") elif x == 10: print("x is equal to 10") else: print("x is less than 10") Functions Functions encapsulate reusable code. Example: def greet(name): print(f"Hello, {name}!") greet("Alice") Output: Hello, Alice! Functions in Python A function is a block of reusable code that performs a specific task. Defining a Function def greet(name): return f"Hello, {name}!" Calling a Function Nested Loops Nested loops are loops inside another loop. Example: for i in range(2): for j in range(3): print(f"i={i}, j={j}") Debugging Programs Debugging is the process of finding and fixing errors. Example: Code with Error: x=5 y = "10" print(x + y) # TypeError Solution: print(x + int(y)) # Converts string to integer Sample Exam Questions 1. Multiple Choice: Which of the following is a valid Python variable name? o A) 1variable o B) variable1 o C) variable-1 o D) def Answer: B 2. Debug the Code: Fix the error in this program: for i in range(5) print(i) Answer: Add a colon after range(5). for i in range(5): print(i) 12. Useful Links Python Official Documentation https://www.w3schools.com/python/ https://www.youtube.com/watch?v=_xQNeOTRyig&list=P LEiEAq2VkUUJO27b6PyoSd7CJjWIPyHYO https://www.youtube.com/watch?v=DMw8jH0R6Fg https://www.youtube.com/watch?v=kqtD5dpn9C8 Section B- Practice to Prepare YOU The following problems are sample problems that you may see on the exam. Section B: Practice Questions Variables and Data Types 1. Declare and Initialize Variables: Declare variables to store a person's name, age, and height. Initialize them with appropriate values. 2. Data Type Identification: What are the data types of the following values: 42, 3.14, "Hello", True, None? 3. Type Conversion: Convert the string "123" to an integer and the float 3.14 to an integer. Operators and Expressions 4. Arithmetic Operations: Write a Python expression to calculate the area of a circle with radius 5. 5. Comparison Operators: Write a Python expression to check if a number is greater than 10 and less than 20. 6. Logical Operators: Write a Python expression to check if a number is both positive and even. Control Flow 7. Conditional Statements: Write a Python program to check if a number is positive, negative, or zero. 8. For Loop: Write a Python program to print the numbers from 1 to 10. 9. While Loop: Write a Python program to print the numbers from 10 to 1. 10. Nested Loops: Write a Python program to print a multiplication table. Functions 11. Function Definition: Define a Python function to calculate the factorial of a number. 12. Function Call: Call the factorial function and print the result for a given input. 13. Function with Parameters: Define a Python function to check if a number is prime. Strings 14. String Operations: Write a Python program to reverse a string. 15. String Slicing: Extract a specific substring from a given string. 16. String Formatting: Format a string to display a name and age in a specific format. Lists and Tuples 17. List Operations: Create a list of 5 fruits, add a new fruit to the list, and remove the first fruit. 18. Tuple Operations: Create a tuple of 3 colors and print each color. 19. List Comprehension: Create a list of squares of numbers from 1 to 10 using list comprehension. Dictionaries 20. Dictionary Operations: Create a dictionary to store information about a person (name, age, city). Access and print the person's age. Answers for section B: Variables and Data Types 1. Answer: Python name = "Alice" age = 30 height = 1.65 2. Answer: o 42: Integer o 3.14: Float o "Hello": String o True: Boolean o None: NoneType 3. Answer: Python str_num = "123" int_num = int(str_num) float_num = 3.14 int_float = int(float_num) Operators and Expressions 4. Answer: Python radius = 5 area = 3.14159 * radius * radius 5. Answer: Python number = 15 is_in_range = number > 10 and number < 20 6. Answer: Python number = 12 is_positive_even = number > 0 and number % 2 == 0 Control Flow 7. Answer: Python number = -5 if number > 0: print("Positive") elif number < 0: print("Negative") else: print("Zero") 8. Answer: Python for i in range(1, 11): print(i) 9. Answer: Python i = 10 while i >= 1: print(i) i -= 1 10. Answer: Python for i in range(1, 11): for j in range(1, 11): print(i * j, end="\t") print() Functions 11. Answer: Python def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) 12. Answer: Python result = factorial(5) print(result) 13. Answer: Python def is_prime(num): if num