CS 1301 FINAL STUDY GUIDE

Topic

Definition

Purpose

Example

Python Intro

A high-level, readable programming language.

To write programs for automation, web dev, data analysis, etc.

print("Hello, world!")

Variables

Containers for storing data values.

To store and reuse values throughout a program.

x = 10

Types

Classifications of data (int, float, str, etc.).

To define what kind of data is being used.

age = 18 (int), name = "Sam" (str)

Branching

Allows decision-making using conditions.

To execute different code paths based on logic.

if x > 10:\n print("Big")

While Loop

Repeats a block of code as long as a condition is true.

Useful when the number of iterations isn't known.

while x < 5:\n x += 1

For Loop

Iterates over a sequence (like a list or range).

To repeat tasks a known number of times or over collections.

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

Functions

Reusable blocks of code with a name.

To organize code and perform tasks multiple times.

def greet():\n print("Hi!")

Strings

Text data enclosed in quotes.

To store and manipulate text.

msg = "Hello"

Lists / Dicts

List: ordered collection; Dict: key-value pairs.

To store multiple items in a single variable.

fruits = ["apple", "banana"], person = {"name": "Alice"}

Classes

Blueprint for creating objects (OOP).

To model real-world entities and use object-oriented principles.

class Dog:\n def __init__(self, name):\n self.name = name

Exceptions

Errors caught and handled during execution.

To make code more robust by handling errors gracefully.

try:\n x = 1/0\nexcept ZeroDivisionError:

Modules

Files containing Python code (functions, classes, etc.).

To organize and reuse code across files.

import math\nprint(math.sqrt(4))

Files

Input/output operations on text/binary files.

To read from or write to external files.

with open("file.txt") as f:\n print(f.read())

Plotting

Visual representation of data (often using libraries).

To analyze and visualize data.

import matplotlib.pyplot as plt\nplt.plot([1,2,3],[4,5,6])\nplt.show()

I.Introduction to Python 

  • Python is a programming language that is used for algorithms and machine learning. 

  • Python is both compiled and interpreted. 

  • Syntax error: when the program contains invalid code that can't be understood 

  • Identifiers are variable names. Reserved words, however, cannot be used because they already have a specific purpose. 

  • Arithmetic operators: Normal division keeps the remainder (/), while Floor division removes the remainder (//)


II.Variables / data types 

  • Python core data types: Lists, integers, strings, dictionaries, tuples, boolean, sets. 

  • Len counts the number of elements or characters in a list or string

  • When indexing using negatives, you do not start from 0 as you would with regular indexing. Instead, you start with 1 and countdown. 

  • The insert method (used in lists) assigns an index to a value. So (2,67) would replace index 2 with the value 67. 

  • If you multiply a list by an integer, the list will duplicate itself, not actually multiply the values by the integer. 

  • Tuples cannot be updated or changed. They store finalized data. 

  • The insert method can only be used with a list. So if you wanted to update a tuple, for example, you would have to convert the tuple to a list.

  • Ex: my_list = list(tuple_name) 

  • my_list.insert(3,20)

  • To delete a value from a dictionary, use del to accomplish it. Ex: def d1[“John”] Remember, use brackets instead of parenthesis. 

  • Retrieval: d1.get(“Susan”) → gives us the value or age (42) 

  • To check if a list contains a certain value, use the following syntax: 15 in my_list


III. If statements 

  • If statements do not require else statements, however, else statements must have an accompanying if statement in order to be appropriately used. 

  • Blocks are represented by indentation. 

  • = is used to assign a data type to a variable, while == is used to write statements within if, for, or while loops. 

  • If your if statement is correct, It will execute the code below it, if not, however, it will move on to else statements.