


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Course: Computer Science 101 Level: Introductory / Diploma Format: Exam Revision Guide
Guide Overview: This document covers essential Python concepts required for university-level introductory programming examinations, including basic syntax, data structures, control structures, functions, and object-oriented principles.
Python is a high-level, interpreted, dynamically typed programming language created by Guido van Rossum. Key features include readability, dynamic typing, and a vast standard library.
Variables: Named memory locations used to store data values. Python assigns variable types dynamically upon assignment. Primitive Data Types: Includes int (integers), float (decimals), str (text strings), and bool (True/False). Type Casting: Explicit conversion using built-in functions: int(), float(), str().
"Docsity" # str is_active = True # bool # Explicit casting num_str = "42" num_int = int(num_str)
Control flow structures direct the execution path of a program based on conditional logic and loops.
Structure Description Syntax Example
If-Elif-Else Executes code blocks based on conditionaltruth statements.^ if x > 5:print("High")
For Loop Iterates over a sequence (list, tuple,dictionary, set, or string).^ for i in range(5):print(i)
While Loop Repeats execution as long as a specifiedcondition holds true.^ while x > 0:x -= 1
break: Terminates the loop statement and transfers execution to the statement immediately following the loop. continue: Causes the loop to skip the remainder of its body and immediately retest its condition. pass: Used as a placeholder when a statement is required syntactically but no code needs to be executed.
Module 3: Core Data Structures
Python provides four primary built-in collection data types that allow structured storage and manipulation of data collections.
Data Structure Ordered Mutable Duplicates Allowed Syntax Format
List Yes Yes Yes my_list = [1, 2, "apple"]
Tuple Yes No Yes my_tuple = (1, 2, 3)
Set No Yes No my_set = {1, 2, 3}
Dictionary Yes (3.7+) Yes Keys: No, Values: Yes my_dict = {"a": 1, "b": 2}
fruits.append("cherry") # Dictionary Access student = {"name": "Alex", "gpa": 3.8} print(student.get("name")) # Output: Alex
Module 4: Functions & Modular Code
Functions are reusable blocks of code designed to perform a single related action. They promote modularity and code reuse.
Defining Functions: Created using the def keyword followed by the function name and parentheses. Parameters vs. Arguments: Parameters are variables defined in the function signature; arguments are actual values passed when called. Return Statement: Uses return to send results back to the caller. Returns None if omitted. Lambda Functions: Anonymous single-line functions written as lambda args: expression.
"""Calculates rectangle area.""" return length * width # Calling function with keyword arguments area = calculate_area(length=10, width=4)