Basics of Python
Rohan Bidarkota
https://slides.com/rohan_bidarkota/python
Programming Architecture
Parts of a Program
- Input - Variables and Data Structures
- Processing - Functions, Control Flow Logic & Loops
- Output - Results (Returns and prints)
Overview
- Introduction
- Data types
- Data structures
- Control flow statements
Why Python?
- Dynamic & readable
- General purpose: there's nothing Python can't do
- Community support: libraries, documentation, online support (StackOverflow, IRC, etc)
- Good "first language" to begin programming in
Logistics
- Python 3.7
- Anaconda for Windows - a Python distribution that bundles the installation of several libraries
- Spyder IDE
Syntax
print ("Hello World")
print ("
Try this out. Print your own name.
s1 = "Hello World"
s2 = "The quick brown fox jumped over the lazy dog"
s3 = "Oh no, what has he done today?!"
print (s1)
print (s1 + s2)
name = "Rohan Bidarkota"
print (name.split(' '))
Strings
Integers
# Try adding two integers
x = 3
y = 10
sum = x + y
print (sum)
# Another example:
a = 23
b = 67
print (a+b)
Float
x = 3.0
y = 4.5
print (x+y)
Now try,
x = 3
y = 4.5
a = 6.0009000090
b = 2.1
print (a+b)
Data Structures
- Used to group and organize data together
- Usually for similar or related data
- Makes processing data easier
Lists
# This is what a list looks like:
x_list = []
y_list = [1, 2, 3, 4]
# You can combine two lists:
print(x_list + y_list)
# You can add elements to a list:
x_list.append(3)
# Remove an element from a list:
y_list.remove(4)
# What happens if you do the following?
y_list.remove(100)
# position, value to be inserted
x_list.insert(4, 6)
# Count the occurrence of an element in the list
x_list.count(4)
# Sort a list
x_list.sort()
# Let's work with some data
# Addresses: Street, City, ZIP code
address1 = ['4400 University Drive', 'Fairfax', 22031]
address2 = ['9490 Blake Ln', 'Fairfax' , 22031]
address3 = ['9110 Lee Hwy', 'Fairfax', 22030]
address_data.append(address1)
address_data.append(address2)
address_data.append(address3)
print (address_data)
Dictionaries
# This is how you define a dictionary
bank_account = {'jack': 1, 'john': 2, 'michelle': 3}
print (bank_account['jack'])
print (bank_account['jane'])
# Add a key-value pair
bank_account['jane'] = 4
# Check if a given key-value pair exists
print ('jane' in bank_account)
# Check what the dictionary has
k = list(bank_account.keys())
print (k)
Sets
# Defined as:
lakers = {'ingram', 'russell', 'black', 'robinson'}
print (lakers)
a = set('abcdefg')
print (a)
b = set('acdf')
print (b)
# letters in a but not b
print (a-b)
# letters in a and b both
print (a&b)
# letters in either a or b
print (a | b)
# letters in a or b but not both
print (a^b)
Control Flow Statements
- if...else
- for loop
- break, continue
- while
- with
...and a few others
if...else statement
# read a number from the user
n = int(input("Please enter an integer"))
if n < 0:
print ("You entered a negative number")
elif n == 0:
print ("You entered zero")
else:
print ("You entered: ", n)
for loop
# Let's iterate over a list
pets = ['cats', 'dogs', 'guinea pigs', 'lizards']
for p in pets:
print (p, len(p))
# Print a lot of numbers
# Try changing the range
for i in range(10):
print (i)
# Range with a start and end
for i in range(5, 9):
print (i)
while loop
# While some condition is true, do something
count = 0
while (count < 9):
print ("Current count:", count)
count = count + 1;
# Use else with while
count = 0
while count < 10:
print ("Count is ", count)
else:
print ("Count has exceeded 10")
break & continue
# break statement
n = 0
for n in range(9):
n = n + 1
if (n == 5):
break;
print ("n :", n)
print ("Outside the for loop")
# continue statement
n = 0
for n in range(9):
n = n + 1
if (n == 7):
continue;
print ("n : ", n)
Functions
- Units of code that achieve specific goals
- Can accept input and offer output
- Usually small, self-contained pieces of logic
# function definition
def hello():
print ("Hello World!")
# function call
hello()
# let's give a function some input
def add(a, b):
print (a+b)
add(3, 5)
# let's get some output from a function
def square_number(n):
return n*n
sq = square_number(10)
print (sq)
PythonWorkshop
By Rohan Bidarkota
PythonWorkshop
Workshop at Digital Scholarship Center, Fenwick Library
- 2,086