Python Programming Course - Intro to Programming PDF
Document Details
2024
Tags
Summary
This document is an introductory Python programming course. It explains basic computer programming concepts and provides an overview of Python. The course material includes sections on computer hardware, programming concepts, and how to write and run simple computer programs.
Full Transcript
17/09/2024 Intro to Programming Chapter 1 Computers want to be helpful... Computers are built for one purpose - to do things for us But we need to speak their language to describe what we want done Users have it easy - someone al...
17/09/2024 Intro to Programming Chapter 1 Computers want to be helpful... Computers are built for one purpose - to do things for us But we need to speak their language to describe what we want done Users have it easy - someone already What Next? What Next? What Next? put many different programs (instructions) into the computer and What What What Next? Next? Next? users just pick the ones we want to use 1 17/09/2024 Users.vs. Programmers Users see computers as a set of tools - word processor, spreadsheet, maps, to-do list, email, etc. Programmers learn the computer “ways” and the computer language Programmers have some tools that allow them to build new tools Programmers sometimes write tools for lots of users and sometimes programmers write little “helpers” for themselves to automate a task Why be a programmer? To get some task done - we are the user and programmer Help analyze and process data (many programs have APIs) To produce something for others to use – write programs Fix an issue with a program 2 17/09/2024 What is Code? Software? A Program? A sequence of stored instructions It is a little piece of our intelligence in the computer It is a little piece of our intelligence we can give to others - we figure something out and then we encode it and then give it to someone else to save them the time and energy of figuring it out A piece of creative art - particularly when we do a good job on user experience while music is playing: Left hand out and up Programs for Right hand out and up Flip Left hand Flip Right hand Humans... Left hand to right shoulder Right hand to left shoulder Left hand to back of head Right hand to back of head Left hand to right hit Right hand to left hit Left hand on left bottom Right hand on right bottom Wiggle Wiggle Jump 3 17/09/2024 Program for a Computer name = raw_input('Enter file:') handle = open(name, 'r') text = handle.read() words = text.split() counts = dict() for word in words: counts[word] = counts.get(word,0) + 1 bigcount = Nonebigword = None for word,count in counts.items(): if bigcount is None or count > bigcount: bigword = word bigcount = countprint bigword, bigcount Hardware Architecture 4 17/09/2024 Generic Computer Computer Input Central and Output Processing Devices Unit Secondary Memory Main Memory Definitions Central Processing Unit: Runs the Program - The CPU is always wondering “what to do next”? Not the brains exactly – not smart but very very fast Input Devices: Keyboard, Mouse, Touch Screen Output Devices: Screen, Speakers, Printer, DVD Burner Main Memory: Fast small temporary storage - lost on reboot - aka Random Access Memory (RAM) Secondary Memory: Slower large permanent storage - lasts until deleted - disk drive / memory stick 5 17/09/2024 Motherboard Source: http://commons.Wikimedia.org Central Processing Unit (CPU) Source: http://commons.Wikimedia.org 6 17/09/2024 Memory (RAM) Source: http://commons.wikimedia.org Hard Drive Source: http://commons.Wikimedia.org 7 17/09/2024 Python as a Language Python: A Computer Language We need to learn the Python language so we can communicate our instructions to Python. In the beginning we will make lots of mistakes When you make a mistake, the computer does not think you are “cute”. It says “syntax error” - given that it “knows” the language and you are just learning it. You must remember that you are intelligent and can learn - the computer is simple and very fast - but cannot learn - so it is easier for you to learn Python than for the computer to learn English... 8 17/09/2024 Talking to Python : The Interpreter The Prompt... 9 17/09/2024 Elements of Python Vocabulary / Words - Variables and Reserved words (Chapter 2) Sentence structure - valid syntax patterns (Chapters 3-5) Story structure - constructing a program for a purpose Reserved Words You can not use reserved words as variable names / identifiers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print 10 17/09/2024 Statements x=2 Assignment Statement x=x+2 Assignment with expression print x Print statement Variable Operator Constant Reserved Word Python Scripts Interactive Python is good for experiments and programs of 3-4 lines long But most programs are much longer so we type them into a file and tell python to run the commands in the file. In a sense we are “giving Python a script” As convention, we add “.py” as the suffix on the end of these files to indicate they contain Python (.ipynb for Jupyter notebooks) 11 17/09/2024 Interactive vs. Script Interactive - You type directly to Python one line at a time and it responds Script - You enter a sequence of statements (lines) into a file using a text editor and tell Python to execute the statements in the file Program Steps and Program Flow Like a recipe or installation instructions, a program is a sequence of steps to be done in sequence Some steps are conditional Sometimes a step or group of steps are to be repeated Sometimes we store a set of steps to be used over and over as needed several places throughout the program 12 17/09/2024 Sequential Steps x=1 x=1 print x print (x) x= x +1 x=x+1 print (x) print x When a program is running, it flows from one step to the next. We as programmers set up “paths” for the program to follow. Chapter 2 x=5 Conditional Steps Yes X < 10 ? No print 'Smaller' Yes X > 20 ? No print 'Bigger' print 'Finis' Chapter 3 13 17/09/2024 n=5 Repeated Steps No Yes n>0? Program: print n n=5 while n > 0 : n = n -1 print n n=n– 1 print 'Blastoff!' print 'Blastoff' Loops (repeated steps) have iteration variables that change each time through a loop. Often these Chapter 5 iteration variables go through a sequence of numbers. Stored (and reused) Steps Program: def hello(): print 'Hello' def hello(): Output: print 'Fun' print 'Hello' print 'Fun' Hello hello() hello() Fun print 'Zip‘ Zip print 'Zip' hello() Hello Fun hello() We call these reusable pieces of code “functions”. Chapter 4 14 17/09/2024 Setting up Python You can download Python at no charge from www.python.org - Launch the terminal on your computer - Type the commande: python. Now you entered the interpreter mode and you can start playing with python 15