Docsity

Introduction to Python Programming: A Beginner's Guide, Lecture notes of Computer Science

Partial preview of the text

Download Introduction to Python Programming: A Beginner's Guide and more Lecture notes Computer Science in PDF only on Docsity!

Python Programming

Introduction

Python for Everybody www.py4e.com

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 put many different programs (instructions) into the computer and users just pick the ones they want to use What Next? What Next? What Next? What Next? What Next? What Next? What Next?

name = input('Enter file:') handle = open(name) counts = dict() for line in handle: words = line.split() for word in words: counts[word] = counts.get(word,0) + 1 bigcount = None bigword = None for word,count in counts.items(): if bigcount is None or count > bigcount: bigword = word bigcount = count print(bigword, bigcount)

python words.py

Enter file: words.txt

to 16

python words.py

Enter file: clown.txt

the 7

Software Input and Output Devices Central Processing Unit Main Memory Secondary Memory

Generic

Computer

What Next? if x< 3: print

Python as a Language

Python is the language of the Python Interpreter and those who can converse with it. An individual who can speak Python is known as a Pythonista. It is a very uncommon skill, and may be hereditary. Nearly all known Pythonistas use software initially developed by Guido van Rossum.

Talking to Python

csev$ python

Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType

"help", "copyright", "credits" or "license" for more information.

What

next?

What Do We Say?

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 cannot use reserved words as variable names / identifiers

False class return is finally None if for lambda continue True def from while nonlocal and del global not with as elif try or yield assert else import pass break except in raise

Sentences or Lines

x = 2

x = x + 2

print(x)

Variable Operator (^) Constant Function

Assignment statement

Assignment with expression

Print statement

Python Scripts

  • Interactive Python is good for experiments and programs of 3-4 lines

long.

  • 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 a convention, we add “.py” as the suffix on the end of these files to

indicate they contain Python.

Interactive versus 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