1 of 17

Python Intro

Original Presentation by Grace (last year’s VP)

Adapted a bit by Elliott

  • Cuz they don’t teach you python anymore…

2 of 17

What is Python?

  • Coding language
    • One of the easiest for beginners to learn
    • Very basic syntax
  • Best way to learn is by using it
  • Super widespread
  • Common to see in CTF challenges
  • Useful for solving a lot of CTF challenges

3 of 17

How do I install Python?

If you have a school Chromebook:

  • Open Ion
  • Open OnDemand
  • Pick an editor
  • Create a new Python file

Or, switch to a Dell/bring a personal computer!

  • If you’re taking a CS class this year, you should do this anyway

Alternative solutions: use an online Python compiler

Otherwise:

  • https://www.python.org/downloads/
  • Popular code editors:
    • Visual Studio Code (what I use)
    • JGrasp (CS class editor)
    • PyCharm
    • IDLE (default editor)
    • Jupyter Notebook (also what I use)

4 of 17

The basics

Using Python, variables, math, logic, loops, functions, and data structures

5 of 17

Using Python

← input

← output

On the terminal:

Running files:

Result:

6 of 17

Variables

  • Labels for storing data
  • Different types:
    • String
    • Integer
    • Float (decimal number)
    • Boolean (true/false)
    • Etc.

​

→ prints “Alice Smith”

7 of 17

Math

  • Kind of self-explanatory

+ ← addition

- ← subtraction

* ← multiplication

/ ← division

// ← integer division (truncates decimal place)

% ← modulo (remainder)

** ← exponent

  • Can also be performed on strings

8 of 17

Conditionals

  • If, elif, else
  • Evaluates an expression to a boolean
  • Uses colons afterward

9 of 17

Loops

Two types of loops: for-loops and while-loops

For loops:

  • Iterate over an iterable (range object, string, list, etc.)

​

While loops:

  • Continues until the condition is no longer satisfied

10 of 17

Functions

  • Reusable blocks of code
  • Take parameters (input) and can return something

11 of 17

Data Structures

There are 4 main types of built-in Python data structures: lists (arrays), dictionaries, tuples, and sets.

← Arrays use [ ]

← Dictionaries use { }

← Tuples use ( )

← Sets also use { }

12 of 17

Data Structures: Lists

  • A 1D array that can store multiple data types
  • Ordered by insertion
    • Elements can be accessed by index; indices begin at 0
  • Mutable (aka can be changed after creation)

What is slicing?

  • Allows you to extract pieces of a list
  • [ start : stop : step ]
  • stop is not inclusive
  • Makes another array

13 of 17

Data Structures: Dictionaries

  • Key-value pairs
    • Keys must be of a hashable, mutable data type (strings, numbers, tuples, etc.) and must be unique!
  • Stay in insertion order (Python 3.7+)
  • Retrieve values using keys
    • e.g. myDict[key1] returns the value that corresponds to key1
  • Can contain multiple data types

14 of 17

Data Structures: Tuples & Sets

Tuples:

  • Immutable, hashable arrays
    • Also indexed
    • Allow duplicates
    • Can’t be changed once they’re created

Sets:

  • Cross between an array and a dictionary
    • Mutable (can change after creation)
    • Don’t allow duplicates
    • Use hashing for order (not in order of insertion)

15 of 17

Libraries

  • Collections of pre-written code
    • Include modules, functions, etc.
  • Examples: NumPy, pandas, matplotlib, scikit-learn, Flask, tensorflow, etc.
  • Typically installed with pip and imported into Python scripts

​

​

​

16 of 17

Some last useful commands

  • Chr(int) returns ASCII character

​

  • Ord(character) returns ASCII number

​

  • String.encode/decode – conversions to and from byte strings

17 of 17

Resources

Do your own research!

https://www.w3schools.com/python/python_intro.asp ← Python intro

https://codingbat.com/python ←excellent beginner Python practice

https://www.programiz.com/python-programming/online-compiler/ ← online Python compiler

https://docs.pwntools.com/en/stable/install.html ←pwntools, a CTF framework and exploit development library

Go solve challenges!