Innovation with Python: Introductory Programming Topics

design based thinking defining possibilities n.w
1 / 29
Embed
Share

Dive into the world of Python programming with a focus on design-based thinking and innovation. Learn about typical introductory topics for Python, running code interactively, functions, and built-in functions. Explore user-defined functions to create repeatable code and understand how to run code interactively via the command line. Get ready to unleash your creativity through Python programming.

  • Python Programming
  • Design-Based Thinking
  • Innovation
  • Introductory Topics
  • Interactive Coding

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Design-Based Thinking: Defining Possibilities for Innovation Introduction to Python Programming Lesson 3 Developed & Delivered by: Jim Cody jcodygroup@gmail.com www.axiomlearningsolutions.com 1

  2. TYPICAL INTRODUCTORY TOPICS FOR PYTHON Environment Interactive, Scripts, IDE Basic Syntax Reserved words, variables, comments, input Functions A block or organized, reusable code (define & call) Modules A file of Python code Variable Types String, Numeric, Lists, Tuples, Dictionaries If Else Statements Loops For, While Files I/O Text, csv 2

  3. RUNNING CODE INTERACTIVELY print "Hello World!" print "Hello Again" print "I like typing this." print "This is fun." print 'Yay! Printing.' print "I'd much rather you 'not'." print 'I "said" do not touch this.' 3

  4. RUNNING A SCRIPT 4

  5. FUNCTIONS

  6. BUILT-IN FUNCTIONS 6

  7. BUILT-IN FUNCTIONS CONTAIN METHODS 7

  8. USER DEFINED FUNCTION REPEATABLE CODE >>>def get_weather(): ... import random ... possibilities = ["rain",'snow','sleet','fog','sun','one never knows'] ... return random.choice(possibilities) ... >>>description = get_weather() >>>print "The weather today will be: ", description The weather today will be: one never knows In interactive mode, the function disappears when the session is closed. 8

  9. RUNNING CODE INTERACTIVELY COMMAND LINE >>> import random >>> possibilities = ["rain",'snow','sleet','fog','sun','one never knows'] >>> r = random.choice(possibilities) >>> print "The weather is ", r The weather is sun >>> >>> r = random.choice(possibilities) >>> print "The weather is ", r The weather is one never knows >>> >>> r = random.choice(possibilities) >>> print "The weather is ", r The weather is rain 9

  10. USER-DEFINED FUNCTION Created within a script (now referred to as a module) Pass the choice out to the main code 10

  11. CALLING THE FUNCTION FROM ANOTHER PROGRAM Return random.choice(possibilities) from module 11

  12. MAIN PROGRAM AND FUNCTIONS >>> import module >>> code >>> code >>> call module.function >>> code >>> code >>> code >>> code >>> code >>> code >>> call module.function >>> code >>> code >>> code >>> code >>> code for module >>> code for module >>> return value from module >>> code for module >>> code for module >>> return value from module >>> just goes to execute something 12

  13. IMPORT OPTIONS >>> code >>> code >>> code >>> import report as abc >>> code >>> code >>> code >>> code >>> code >>> code >>> from report import get_x as abc >>> code >>> code >>> code >>> code Rename the module Import a section of the module 13

  14. DATA TYPES

  15. DATA TYPES Strings Numbers Boolean Lists Tuples Dictionaries 15

  16. FILE I/O

  17. READ A CSV FILE # explicitly declare built-in module to read csv files import csv """ open the first attendance CSV file, store a reference to this open file as f and then pass this reference to the csv module. """ # parse the open file, and return a parsed list of rows. csv_f = csv.reader(open('attendees1.csv )) # each_row is just a variable name for each_row in csv_f: print each_row 17

  18. READ A CSV FILE - TWICE # explicitly declare built-in module to read csv files import csv # parse the open file, and return a parsed list of rows. csv_f = csv.reader(open('attendees1.csv )) # each_row is just a variable name for each_row in csv_f: print each_row We need to open and read the file again csv_f = csv.reader(open('attendees1.csv )) for row in csv_f: print row[2] # just print the email 18

  19. READ A CSV FILE CREATE A NEW LIST # explicitly declare built-in module to read csv files import csv # create a list from the file data csv_f = csv.reader(open('attendees1.csv )) attendee_emails1 = [] # this create an empty list for row in csv_f: attendee_emails1.append(row[2]) # this populates the list as one long row print attendee_emails1 19

  20. READ A CSV FILE CREATE A 2ND NEW LIST import csv csv_f = csv.reader(open('attendees1.csv )) attendee_emails1 = [] # this create an empty list for row in csv_f: attendee_emails1.append(row[2]) # this populates the list as one long row # do the same with the second file # create a list from the file data csv_f = csv.reader(open('attendees1.csv )) attendee_emails2 = [] # this create an empty list for row in csv_f: attendee_emails2.append(row[2]) # this populates the list as one long row 20

  21. CREATE A SET # convert the list to a set # A set is like a list but... there are no duplicates # There is no order # Converting to a set allows us to make a comparison EXAMPLE >>> small_set = set([2, 3, 4, 5, 6]) >>> large_set = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> print large_set.difference(small_set) set([8, 1, 9, 7]) 21

  22. 22

  23. READ A CSV FILE CREATE A 2ND NEW LIST import csv csv_f = csv.reader(open('attendees1.csv )) attendee_emails1 = [] for row in csv_f: attendee_emails1.append(row[2]) csv_f = csv.reader(open('attendees1.csv )) attendee_emails2 = [] # this create an empty list for row in csv_f: attendee_emails2.append(row[2]) email_set1 = set(attendee_emails1) email_set2 = set(attendee_emails2) second_year_attendees = email_set2.difference(email_set1) print second_year_attendees 23

  24. WRITE A CSV FILE 24

  25. 25

  26. 26

  27. 27

  28. 28

  29. 29

Related


More Related Content