1 of 28

Day 2 - Intro to Python Workshop

Wednesday 8B | Room 57

9/10/2025

2 of 28

Downloading Python

www.python.org/downloads/

3 of 28

Online python environment

online-python.com

4 of 28

Operators

  • Symbols that specify a certain computation (ex. addition)
    • Arithmetic: +, -, /, *, %, **
    • Comparison: >, <, >=, <=, !=, ==
  • Examples:

1 >= 3 → False

2 == 2 → True

4 != 6 → True

4 + 1 → 5

7 % 3 → 1

3 ** 2 → 9

5 of 28

Variables

  • Storage location for a piece of data
  • Value can be changed at any time
  • Assigned using the “=” operator
  • Variable names:
    • Must begin with letter or underscore (“_”)
    • Case sensitive (capitalization counts!)

6 of 28

Examples of Variables

7 of 28

Examples of Variables

8 of 28

Examples of Variables

9 of 28

Examples of Variables

10 of 28

Strings

  • A sequence of characters (letters, numbers, punctuation, etc.)
  • Represented using quotes (ex. “abc”)
  • Example:
    • X = “hello” → the variable X now has the value hello
  • len(string) function used to get length of string

11 of 28

If-Else

  • If, elif, and else statements are used to implement conditional program behavior
  • Syntax:

  • elif and else are not required (used to add additional conditional statements)

12 of 28

Examples of If-Else Clauses

13 of 28

Examples of If-Else Clauses

14 of 28

Lists

  • A general purpose 1-dimensional container for variables
  • Represented using square brackets (ex. [a, b, c])
  • Can change in size
  • Elements can be added or removed
  • len(list) function used to get length of list

15 of 28

Examples of Lists

16 of 28

Examples of Lists

17 of 28

List Indices

  • Elements in a list are accessed by an index number
  • Index numbers start at 0
  • List: x = [“a”, “b”, “c”, “d”, “e”]
  • First element: x[0] → 'a'
  • Nth element: x[2] → 'c'
  • Last element: x[-1] → 'e'
  • Next-to-last: x[-2] → ‘d’

18 of 28

List Slicing

  • Taking only a section of a larger list
  • Slice syntax: x[start : end : step]
  • The start value is inclusive, and the end value is exclusive
  • Step is optional and defaults to 1.
  • Leaving out the end value means “go to the end”
  • Slicing always returns a new list copied from the existing list

19 of 28

List Slicing

  • List: x = [“a”, “b”, “c”, “d”, “e”]
  • x[0 : 1] → ['a']
  • x[0 : 2] → ['a','b']
  • x[-3 :] → ['c', 'd', 'e']
  • x[2 : 5 : 2] → ['c', 'e']

20 of 28

List Assignments and Deletion

  • Lists can have their elements overwritten or deleted
  • del command
  • List: x = [“a”, “b”, “c”, “d”, “e”]
  • x[0] = -3.14 → x is now [-3.14, “b”, “c”, “d”, “e”]
  • del x[0] x is now [“b”, “c”, “d”, “e”]

21 of 28

Loops

  • A sequence of instruction s that is continually repeated until a certain condition is reached
  • Two types:
    • While loops
    • For loops

22 of 28

While Loops

  • While loops have a condition and a code block
  • Example:

23 of 28

For Loops

  • Often used to loop through each element of list/string
  • Example:

24 of 28

range() Function

  • Auto-generates sequences of numbers that can be used for indices
  • range() syntax: range(start, exclusive end, increment)
  • Examples:
    • range(3) → 0, 1, 2
    • range(0,4) → 0, 1, 2, 3
    • range(-3,15,3) → -3, 0, 3, 6, 9, 12

25 of 28

Examples of range()

26 of 28

Practice Problem Bio in CS

In bioinformatics, a common task is to calculate the GC content of a DNA sequence. GC content is the percentage of Guanine (G) and Cytosine (C) bases in a DNA or RNA sequence. This value can be important for a variety of reasons, such as predicting the stability of DNA.

The Problem

Write a Python program that takes a DNA sequence as a string and calculates its GC content.

27 of 28

Answer

28 of 28

Kahoot Time!!

Get your computers out :D