Introduction to Python

Justin Wu
DBHS Code Club
Published in
3 min readOct 24, 2017

Thank you to everyone who went to our workshop! This is a recap of everything that was taught. If you’re looking for the slides, they are here.

Python 3 is a very popular, powerful, and dynamic interpreted language. Code written in Python is designed to be simple to read. Using https://repl.it/languages/python3, we can run a python on a web browser.

print("Hello, World") # Hello, World program

On repl.it, we have direct access to the interpreter, the black command line tool on the right side that we can use to work directly with Python. However, we can also use the left side to type out our code.

Anything code below with “>>>” implies we are using the interpreter. Ignore the greater than signs, they are only to tell you whether I am using the interpreter or the editor.

>>> "This is a string to be entered using the console." # and this is a comment
'This is a string to be entered using the console.'

Arithmetic

Python’s interpreter can be used as a calculator on the command line for basic arithmetic (and more complex math but that’s for later on).

>>> 3 + 4
7
>>> 3 - 4
-1
>>> 3 * 4
12
>>> 3 / 4
0.75
>>> 3 * 4 + 3 # order of operations
15
>>> 3 * (4 + 3)
21

If you’re not using the interpreter (the code editor on the left side of the repl.it page), you need to put the arithmetic operations around the print() function receive output.

print(3 + 4)

Variables

In Python there is no need to declare a variable’s datatype. Just set it equal to a value and the interpreter will determine its datatype.

>>> var = 47
>>> var - 5
42
>>> knottsIsFun = True
>>> moneyInWallet = 12.50
>>> todo = ["dishes", "laundry"]

Strings

A string in Python is a sequence of characters within single or double quotes (‘…’ or “…”). Strings are immutable (they cannot be changed after they are created). Python creates a new string every time we modify it.

>>> "ayy lmao"
'ayy lmao'
>>> 'ayy' + "lmao" #concatenation of two strings
'ayylmao'
>>> str = "hello"
>>> str + " and goodbye"
'hello and goodbye'

Strings can also be indexed using the “[begin:end:step]” syntax. Python uses zero-based indexing which means that the first character of a string is the zeroth index.

>>> string = "123456789"
>>> string[0] #character at position 1
'1'
>>> string[1:8] # second to eighth character
'2345678'
>>> string[0:8:2]
'1357'
>>> string[::2]
'13579

Challenge: Can you reverse string using this syntax?

Comparison Operators

Also known as relational operators, comparison operators compare two values and decides the relation between them.

>>> 3 == 2 # not a true statement
False
>>> 13 > 4
True
>>> 47 <= 12
False
>>> "Apples" == "Oranges" # strings do not have the same content
False
>>> "Apples" == "Apples"
True

if…elif…else statements

Perhaps the most well known statement is the if…else statement, it tells a computer to run a section of code only if a condition evaluates to True.

canWalk = True
havePermission = True
if(canWalk and havePermission):
print("You can go out!")
elif(canWalk and not havePermission): #if you can walk but have no permission
print("You don't have permission!")
elif(not canWalk and havePermission): #if you have permission but you can't walk
print("You can't walk!")
else: #if you can't walk and have no permission
print("You can't go out!")

Use the code editor (left side) to try this.

User Input

To read data from a user, use the input() function. You can store the results in a variable.

name = input("Enter your name") # asks the user for his name
print("Hello " + name + "!")

Challenge!

With everything you just learned, try to make a basic four function calculator using arithmetic operators!

  • Don’t forget to use the print() function
  • Use an if…elif…else statement with the input() function for operator selection
  • The calculator should take in two values and return one value
  • It only needs to add, subtract, multiply, and divide (four functions)

--

--