Sie sind auf Seite 1von 28

Python

Introduction, Print, Variables,


Numbers and Operators
CIS 415 Lecture 11
Hina Arora

Announcements
Reminder: In-Class Exercise 5 is due at 5pm on 10/2 (Friday).
o How many of you have completed the exercise?
o How many of you are part way?
o How many of you have not started?
o Any issues?

Reminder: First Quiz is on 10/5!


o Quiz will include:
Required Readings called out on Schedule,
Material covered in Lectures 1 through 10, and
In-Class Exercises 1 through 5.

o You are allowed 4 pages (one-sided, 8.5x11) of open notes.

Starting Python Module today. You need to have completed Anaconda (Python 3.4) Installation:
o How many were able to do so on their personal laptops?
o How many had to resort to Citrix VM?
o Anybody who hasnt downloaded Anaconda yet?

Before we start have the following


ready to go

* Per instruction on Blackboard under Course Content -> Week 6 -> Downloading and Installing Anaconda

Well (mostly) be using the Script Editor to write code,


not the Console

Script Editor

Console

Python

https://docs.python.org/3.4/

Python Origins
Named after the show Monty Pythons Flying Circus
Originally created by Guido van Rossum as a hobby
Now supported by the open source community

https://en.wikipedia.org/wiki/Monty_Python

van Rossum continues as "Benevolent Dictator For Life, overseeing


the development process, and making decisions where necessary
Van Rossum went on to work for Google and is now at Dropbox

Review: Program Execution


Paradigms
High Level
Language

Compiler

Machine
Code

Real
Machine

Performance

Interpreter
(VM)

Debuggability

Intermediat
e Language
(Byte Code)

(Interpreter
) VM

Portability

Intermediat
e Language
(Byte Code)

(Interpreter
) VM

High Level
Language

High Level
Language

High Level
Language

Compiler

Compiler

Reference: http://www.i-programmer.info/babbages-bag/352-

JIT

Machine
Code

Programming
Language

Python: Language vs
Implementation
Implementation

Execution

CPython or
Python
(C)

Python
Source
Code

IronPython
(C#)

Python
Source
Code

Jython
(Java)

Python
Source
Code

CPython
Compiler

Bytecode
(.pyc*)

CPython
VM

*typically seen under


_pycache_ dir when you import
modules
IronPython
Compiler

Bytecode

CLR VM

Bytecode

JVM

Python
Jython
Compiler

Type following on Python Console:

Reference: http://www.toptal.com/python/why-are-there-so-

Python coding best practices


The Zen of Python:
https://www.python.org/dev/peps/pep-0020/
Style Guide for Python Code:
https://www.python.org/dev/peps/pep-0008/

Print

follow along!

Create a new file called


Lesson1Print.py
Go to File -> New file
Go to File -> Save as
Create directory called
CIS415
Click into CIS415
directory
Save file as
Lesson1Print.py

Put down your first line of Python code

Execute and View Output

Will these statements work?

Variables
follow along!

Variable Naming Rules


Variable names can be any combination of letters, numbers and _
o Variable names cannot start with a number

Variable names are case sensitive


Variable names cannot use reserved words
o Example of reserved words: print, while, if, elif, else, and, break, class, def,

You should strive to have easily readable, meaningful variable names,


with consistent naming style:
o Example: NumberOfMiles, number_of_hours, speedMPH

Create a new file called


Lesson2Variables.py
Go to File -> New file
Go to File -> Save as
Go to CIS415 directory
Save file as
Lesson2Variables.py

Will these statements work?

Numbers and Operators


follow along!

Operator Precedence
Best to just use brackets () to disambiguate!

Operator precedence in Python, from lowest precedence (least binding) to highest precedence
(most binding). Operators in the same box have the same precedence. Operators in the same box
group left to right (except for comparisons, including tests, which all have the same precedence and
chain from left to right and exponentiation, which groups from right to left).

or

2*3+5

and
not
in, not in, is, is not, <, <=, >, >=, !=, ==
|
^
&
<<, >>
+, *, /, //, %
+x, -x, ~x
**

2 * 3 ** 4

Create a new file called


Lesson3NumOps.py
Go to File -> New file
Go to File -> Save as
Go to CIS415 directory
Save file as
Lesson3NumOps.py

Will these statements work?

Das könnte Ihnen auch gefallen