Session #1 — Project NexTech
Intro to
Python
Rithvik Madiraju, Shounak Ray Chaudhuri
Arshan Shokooki, Ethan Liu
What is Project NexTech?
Project NexTech is a nonprofit organization that began on May 19th, 2023, whose goal is to provide
volunteer-powered, free, and effective STEM education—for students, by students.
We are powered by high school students across United States and we currently have 16 branches in
various high schools and/or robotics teams in San Diego, Irvine, and Palo Alto. We currently have around
70 student volunteers and counting.
We teach various subjects such as build workshops, computer-aided design (CAD), computer
programming, physics and math, and earth science.
This class is called [3.1] Intro to Python, and is a part of NexTech’s Programming department.
What is Python?
→ “Python” originally comes from the name of a BBC
comedy series called “Monty Python’s Flying Circus”
→ Its logo is based on the Python snake
→ Widely-used programming language and computing
platform used for AI applications and data structures
→ Made by Guido van Rossum, released by Python
Software Foundation
→ One of the most popular for developers worldwide
→ Object-oriented programming language
→ Runs on many devices, making it popular
→ Based on the C language in the backend, but has
different syntax
→ Many libraries and softwares made for Python
Object-Oriented Programming (OOP)
→ Python is object-oriented
→ Based on the concept of an “object”
→ Objects are defined with their own properties
→ Objects have abilities; actions they can do
→ OOP involves objects interacting with each other
→ Many other languages are also object-oriented
→ Java, C++, JavaScript, etc.
→ Benefits of OOP:
→ Simpler to implement
→ Reduced software complexity
What is Python used for?
→ Constructing apps for PC, mobile, console, etc.
→ Backend for web applications
→ Artificial Intelligence
→ Python’s mathematical efficiency makes
it good at linear algebra, which is the
core of artificial intelligence (ChatGPT)
→ There are many AI libraries for Python
→ Data Management
→ Data visualization (matplotlib, seaborn)
→ Managing large amounts of data
→ Scientific applications and simulations
→ CLI/GUI programs (automation/pentesting)
→ Robotics
Time Check: 9:45 AM
[15] Syntax
4
Python Syntax
Python is very similar to English and uses many simple words. We will cover important keywords and
operators in Python as we go.
→ Parentheses () are used to hold arguments and they are sometimes left empty
→ Colons : are used to indicate upcoming code or positions in lists
→ Brackets [] and curly brackets {} are used to hold data in lists
→ Dots . are used to trigger functions/actions or access variables
→ Commas , are used to separate items in lists
→ Hashtags # are used to create comments
→ Single equal signs = are used to SET the value of variables
In Python, indentation is used to describe the level/hierarchy of code lines. Often, lines followed by colons
must be indented. Semicolons ; are not used in Python. They can be used in some cases, but code with
semicolons is considered non-Pythonian.
Watch your spelling, spacing, and indentation!
Neat Code vs. Messy Code Good to do:
→ Condense similar statements with
Neat Code — Try to do this: Messy Code — Avoid this: Python’s smart features
→ Group similar parts of your code
together, separated by empty lines
→ Leave comments explaining the
important parts of your code
→ Leave spaces between operations
to make it easier to read
Avoid:
→ Inefficient use of lines (lines 1-3)
→ Forgetting spaces between list
items and operations, making
them hard to read later
→ Bunching all of the code together,
even when there are
different sections
Time Check: 10:00 AM
[30] Variables
5
What is a variable?
A variable is a place in memory that stores data. It has three properties:
1. Name (ex: apples, COOL_VALUE, nexTech, etc.)
Must start with a letter, only have letters/numbers/underscores, be different from Python keywords
2. Value (ex. 12, “hello!”, True, 98.24, etc.)
3. Type (automatically determined) (ex. Integer (int), Float, Boolean (bool), String (str), etc.)
To define a variable, use this format: <name> = <value> (type is implied)
Examples:
number_of_months = 12
pi = 3.141592653589793
shrishIsCool = True
coolPoem = “Roses are red, violets are blue, this is a rhyme, and it doesn’t rhyme.”
reasonsToVoteForMe = {“First One”: “I am cool”, “Second One”: “I love programming”}
PERFECT_SQUARES = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Data Types
There are many data types in Python.
→ Each data type has its own properties and abilities
→ They are used in different cases in different ways
→ Python automatically guesses the data type of values
Some examples:
→ integer (int) → 2023
→ float → 2023.2023 (32-bit)
→ double → 2023.2023 (64-bit)
→ string (str) → “hello world”, ‘NexTech’
→ boolean (bool) → True, False
→ list → [“Shounak”, “Prisha”, “Soham”, “Shubhayan”]
→ tuple → (“pizza”, 12, True)
→ set → {2023, ‘NexTech’, 2023.2023}
→ dictionary (dict) → {“Prisha” : 18, “Shrish” : 12}
Integers (int)
● Integers are whole numbers with no decimal (32-bit)
● The same integers in math
● Mathematical operations can be done to each other
● Examples:
○ 55
○ 0
○ -345
Floats (float)
● Floats are any real number with a decimal (32-bit)
● Mathematical operations can be done between them
● Examples:
○ 5.4
○ -3465.012
○ 2.0
Doubles (double)
● Doubles are any real number with a decimal (64-bit)
● Mathematical operations can be done between them
● Examples:
○ 5.4
○ -3465.012
○ 2.0
Strings (str)
● Strings are used to represent text
● Strings are made up of a nonnegative number of characters
● These characters can be letters, numbers, or symbols
● Strings have single (‘ ’) or double (“ ”) quotation marks for the start and end
○ You must use the same type of quotation marks for both sides
● Some examples:
○ ‘!@$(*&^#$asdf18192857’
○ “e”
○ “”
○ “turtle”
○ ‘3’
Booleans (bool)
● Booleans are either True, False, or null (undefined)
● They are useful for logical operations
● Examples:
○ True
○ False
○ null (undefined)
List (list)
● A collection of other values of many data types
● Ordered (order matters)
● Mutable (values can be changed)
● They are useful for storing information
● Examples:
○ [“Shounak”, “Prisha”, “Soham”, “Subhayan”]
○ [2020, 2021, 2022, 2023]
○ [True, False, True, True]
Tuple (tuple)
● A collection of other values of the same or different data types
● Ordered (order matters)
● Immutable (values cannot be changed)
● They are useful for storing information
● Examples:
○ (255, 255, 0)
○ (“Rithvik”, 10, 16)
○ “Goodbye”, 13.2, 13.3
Set (set)
● A collection of other values of the same or different data types
● Unordered (order doesn't matter)
● Mutable (values can be changed)
● No duplicate items
● They are useful for storing information
● Examples:
○ {“Shounak”, 2023, True, 2.2}
○ {False, “Prisha”, 2023}
Dictionaries (dict)
● A collection of pairs of multiple data types
● Unordered (order doesn't matter)
● Mutable (values can be changed)
● They are useful for defining terms like a dictionary
● Examples:
○ {“Shounak”: 15, “Prisha”: 18}
○ {‘Hello’ : 2, ‘Okay’ : 2, ‘Insane’ : 3}
Data Types Practice
1293 True -0.145 “Tuple” (True, 255, 254) -981 ‘24’ 2013.2356 189.24
False 8002 ‘Int’ {“Dev”, 1267, ‘Bool’} {“A”: 1, “B”: 2.2, “C”: 3.33, “D”: 4.444} (0, 127, 255)
[12.1, 12.2, 12.3, 12.4] {“Jake”, 4, 9.2, False, “Postal Service”, True, -2.333} [True, False, True, True]
→ Name the data type of each value
→ If it’s a collection of values, name the data type of all the values inside it, too
Options: Integer (int), Float/Double, String (str), Boolean (bool), List, Tuple, Set, Dictionary (dict)
(these will be removed after the first few questions!)
Variables
Time to practice!
→ Make four single variables of the main types and four collections of the main types.
→ Print out each of the variables using print().
→ Use the hashtag # to add a comment next to each variable with the type you think it is.
Instructors will be walking around to check and help you if you need it.
Reminders:
Integer (int): -132, 345 Float/Double: 3.14, -0.24 String (str): “Yo”, ‘hi’ Boolean (bool): True, False
List: [1, 2, 3, 4, “Hi”] Tuple: (1, 2, “Hi”) Set: {“Hi”, 1, True} Dictionary (dict): {“Key1”: 10, “Key2”:
20}
Time Check: 10:30 AM
[30] Input/Output
6
Output to Console (printing)
→ Console is a stream where information can be sent
→ The console appears on the right side of the screen
Writing to console:
→ The “print” method can be used
→ Information is placed in parentheses
→ ex. print(“Hello world!”), print(12), print(True)
f-Strings, or formatted strings, can be used to output your own message along with a variable.
→ To output Your age is 12! with the variable age,
→ Do: print(f”Your age is {age}!”)
Try to print a few messages or pieces of information of your own!
Input from Console
→ You can also type into the console for input
Input from console:
→ The “input” method can be used
→ Information is typed in a console line
→ A variable can be made to save input
→ ex. username = input()
→ Prompts can be added, as well, into the parentheses. Prompt must be strings.
→ ex. age = input(“How old are you?”)
Try taking some inputs. Print them out how we did in the last slide, too.
Time Check: 11:00 AM
[30] Lists
7
Accessing Values: Ordered Lists
List positions are counted by index. They start at the “zeroth” position!
list = [“First”, “Second”, “Third”, “Fourth”, “Fifth”, “Sixth”] tuple = (100, 178, 255)
0 1 2 3 4 5 0 1 2
Indexing is used to access and edit elements in a list. You can use the name of the collection and the
index.
To access “First”, it’s list[0]. To access 178, it’s tuple[1]. To access “Third”, it’s list[2].
You can also count backwards:
list = [“First”, “Second”, “Third”, “Fourth”, “Fifth”, “Sixth”] tuple = (100, 178, 255)
-6 -5 -4 -3 -2 -1 -3 -2 -1
To access “Sixth”, it’s list[-1]. To access 255, it’s tuple[-1]. To access “Fifth”, it’s list[-2].
To get the length of an ordered list, the len() method can be used. Example: len(list)
Practice doing this on your computer! Make a list and practice accessing and printing values.
Accessing Values: Ordered Lists (cont’d)
You can also access a range of numbers from ordered collections using the colon : and two positions.
The first position is included. The second position is EXCLUDED. [0:3] accesses positions 0, 1, and 2, NOT 3!
list = [“First”, “Second”, “Third”, “Fourth”, “Fifth”, “Sixth”] tuple = (100, 178, 255)
0 1 2 3 4 5 0 1 2
-6 -5 -4 -3 -2 -1 -3 -2 -1
If you are accessing the first value, the number before the colon can be blank. If you are accessing the
second value, the number after the colon can be blank.
To make a mini-list with the “First”, “Second”, and “Third” values, this can be done:
smallList = list[0:3] OR smallList = list[:3]
To make a mini-tuple with the values 178 and 255, this can be done:
smallTuple = tuple[-2:6] — There is no actual “sixth” index in this case.
It’s an imaginary position outside of the list.
Accessing Values: Ordered Lists (cont’d)
You can also skip values and take “strides” of information. Add another colon and a length for your stride.
list = [“First”, “Second”, “Third”, “Fourth”, “Fifth”, “Sixth”] tuple = (100, 178, 255)
0 1 2 3 4 5 0 1 2
-6 -5 -4 -3 -2 -1 -3 -2 -1
To make a mini-list with the “First”, “Third”, and “Fifth” values, this can be done:
smallList = list[0:6:2] OR smallList = list[::2]
To make a mini-list with the “Second”, “Fourth”, and “Sixth” values, this can be done:
smallList = list[1:6:2] OR smallList = list[1::2]
To make a mini-list with the “Sixth”, “Fourth”, and “Second” values, this can be done:
smallList = list[0:6:-2] OR smallList = list[::-2] — You can make backwards strides!
Accessing Substrings: Strings
You can treat Strings kind of like lists. Their positions work in a similar way.
nonprofit_name = “Project NexTech” → “ P r o j e c t _ N e x T e c h”
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Strings can only be accessed, not edited. They are immutable.
To access “Project”, it’s nonprofit_name[0:7] or [:7] or [0:-8] or [:-8]
To access “NexTech”, it’s nonprofit_name[8:15] or [8:] or [-7:15] or [-7:]
To access “ject Nex”, it’s nonprofit_name[3:11] or [-12:-4]
To access “PoetNxeh”, it’s nonprofit_name[0:15:2] or [-15:15:2] or [::2]
To access “nexNteoP”, it’s nonprofit_name[0:15:-2] or [-15:15:-2] or [::-2]
To get the length of a String, it’s the same as a collection. Example: len(nonprofit_name)
Exercise
Make an ordered list with the values: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
1. Access and print the following values using the index methods we covered:
→ 0, 2, 4, 6, 8, 10
→ 10, 12, 14, 16, 18, 20
→ 0, 4, 8, 12, 16, 20
→ 2, 6, 10, 14, 18
→ 0, 6, 12, 18
→ 6, 8, 10, 12, 14, 16
→ 20, 16, 12, 8, 4, 0
→ 20, 14, 8
Reminders: <list_name>[<start_index>:<end_index>:<stride>], if the start_index is 0, the end_index is
equal to the length, or the stride is 1, it can be left blank. Positions begin from zero!
Let’s see if you can get there in
12 minutes!
Accessing Values: Unordered Lists
One type of unordered list in Python is a set. It contains values of many types that are not the same.
cool_set = {12, -0.14, True, “hello”}
True and 1 are considered the same. False and 0 are considered the same. Duplicates will be ignored.
For all collections, you can also check whether a value is in or not in the collection. For this set, try:
print(12 in cool_set) and print(False not in cool_set)
You can add items to the set by using the .add() method.
You can remove items from the set by using the .remove() method.
You can avoid getting errors if the value was not in the list by using the .discard() method.
Examples: cool_set.add(False), cool_set.remove(True), cool_set.discard(True)
Accessing Values: Unordered Lists (cont’d)
Another type of unordered list in Python is a dictionary (dict). It contains key-value pairs.
aD = {13: “A prime number”, 14: “A composite number”, 16: “A perfect square”}
bD = {“dog”: “A cool animal that barks”, “cat”: “A cool animal that meows”}
To access dictionary values, use the key (the first value in each pair before the colon :)
To access “A prime number”, aD[13] needs to be used.
To access “A cool animal that barks”, bD[“dog”] needs to be used.
You don’t need to know the position of each value in dictionaries.
To add values to a dictionary, use this syntax: <dict name>[<new key>] = <new value>
To add “bird”: “tweet” to the dictionary, this can be done: bD[“bird”] = “A flying animal”
To change the value of dictionary values, use this syntax: <dict name>[<key>] = <new value>
To change the value of “dog”, this can be done: bD[“dog”] = “A cool animal that sniffs”
Exercise
1. Make a set with the values: True, False, “goodbye”, 12, -1.3
→ Print whether True is in the set
→ Print whether 12 is in the set
→ Add the value -10 to the set and remove the value 12
→ Print the set to check if these changes were made
2. Make a dictionary with the values: 20: “4*5”, 30: “2*3*5”, 40: “2*2*2*5”
→ Print the value of the pair with the key 30
→ Add the pair 50: “2*5*5” to the dictionary and change the value of the pair with the key 20 to “2*2*5”
→ Print the dictionary to check if these changes were made
Reminders: Use the “in” keyword to check whether a value is in a list. .add() .remove()
<dict name>[<key>] = <new value>
Let’s see if you can get there in
12 minutes!
Time Check: 11:30 AM
Problem Set
8
Lists and Indexing Problem Set
Explanations can be found on slides 45-54.
1. Make a list with the following values: “One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”,
“Nine”
2. Print “Three” by accessing an index from the list. There are two ways to do this.
3. Print “One”, “Two”, “Three”, “Four” by accessing indices in the list. There are four ways to do this.
4. Print “Six”, “Seven”, “Eight”, “Nine” by accessing indices in the list. There are four ways to do this.
5. Print “Three”, “Four”, “Five”, “Six”, “Seven” by accessing indices in the list. There are two ways to do
this.
6. Print “One”, “Three”, “Five”, “Seven”, “Nine” by using strides of 2 while indexing the list.
7. Print “Two”, “Five”, “Eight” by using strides of 3 while indexing the list.
8. Print “Five”, “Seven”, “Nine” by using strides of 2 while indexing the list.
9. Go through your answers for all of the previous exercises and try to make them as simplified as
possible.
Substrings Problem Set
Explanations can be found on slide 50.
1. Make a string with the following value: “abcdefghi”
2. Print “c” by accessing a substring from the string. There are two ways to do this.
3. Print “abcd” by accessing a substring from the string. There are four ways to do this.
4. Print “fghi” by accessing a substring from the string. There are four ways to do this.
5. Print “cdefg” by accessing a substring from the string. There are two ways to do this.
6. Print “acegi” by using strides of 2 while accessing a substring from the string.
7. Print “b”, “e”, “h” by using strides of 3 while accessing a substring from the string.
8. Print “e”, “g”, “i” by using strides of 2 while accessing a substring from the string.
9. Go through your answers for all of the previous exercises and try to make them as simplified as
possible.
Python Day 1 Overall Practice
You will learn Python the best by practicing independently. Try to experiment with all the things we
covered in the lesson independently. Change values, change the order that your lines of code are in, add
more variables. See what happens!
After finishing the previous problem sets, just spend time messing around with these features of Python
and see what they can do. Come in to the next class with any new information you learned!
If you have any questions, please feel free to contact the instructors with the contact information at the
end of each day’s slides.
Spend as much time as you can (reasonably) before next week’s lesson. It’s best if the student does the
work on their own without parental guidance. If the student is stuck, they can request guidance, but
independent learning and doing the problem sets is the best way to learn.
Thank you!
Time Check: 11:30 AM
Answer Key
9
Lists and Indexing Problem Set
These answers are not necessarily the only correct answers. Many possible answers exist.
1. niceList = [“One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”]
0/-9 1/-8 2/-7 3/-6 4/-5 5/-4 6/-3 7/-2 8/-1
2. print(niceList[2]) or print(niceList[-7])
3. print(niceList[0:4]) or print(niceList[:4]) or print(niceList[-9:-5]) or print(niceList[:-5])
4. print(niceList[5:9]) or print(niceList[5:]) or print(niceList[-4:9]) or print(niceList[-4:])
5. print(niceList[2:7]) or print(niceList[-7:-2])
6. print(niceList[0:9:2]) or print(niceList[::2]) or others
7. print(niceList[1:8:3]) or print(niceList[1::3]) or others
8. print(niceList[4:9:2]) or print(niceList[4::2]) or others
9. Any times you start at the beginning, leave the space before the first colon blank. Any times you end at
the end, leave the space after the first colon blank.
Substrings Problem Set
These answers are not necessarily the only correct answers. Many possible answers exist.
1. niceString = “a b c d e f g h i” # expanded for ease of viewing
0/-9 1/-8 2/-7 3/-6 4/-5 5/-4 6/-3 7/-2 8/-1
2. print(niceString[2]) or print(niceString[-7])
3. print(niceString[0:4]) or print(niceString[:4]) or print(niceString[-9:-5]) or
print(niceString[:-5])
4. print(niceString[5:9]) or print(niceString[5:]) or print(niceString[-4:9]) or
print(niceString[-4:])
5. print(niceString[2:7]) or print(niceString[-7:-2])
6. print(niceString[0:9:2]) or print(niceString[::2]) or others
7. print(niceString[1:8:3]) or print(niceString[1::3]) or others
8. print(niceString[4:9:2]) or print(niceString[4::2]) or others
9. Any times you start at the beginning, leave the space before the first colon blank. Any times you end at
the end, leave the space after the first colon blank.
Thanks — Questions?
Do you have any questions?
[email protected]