A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming



Similar documents
Python Programming: An Introduction to Computer Science

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Python Programming: An Introduction to Computer Science

Introduction to Python

Welcome to Introduction to programming in Python

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

CS 40 Computing for the Web

Introduction to the course, Eclipse and Python

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Computers and Programs

Chapter 13: Program Development and Programming Languages

7 Why Use Perl for CGI?

Exploring Algorithms with Python

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

CS 253: Intro to Systems Programming

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Introduction to Python

Computer Science for San Francisco Youth

The programming language C. sws1 1

Introduction to Python

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Introduction to Python

Outline. multiple choice quiz bottom-up design. the modules main program: quiz.py namespaces in Python

Crash Dive into Python

Welcome to Introduction to Computers and Programming Course using Python

Python Loops and String Manipulation

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

Programming Languages CIS 443

CS 1133, LAB 2: FUNCTIONS AND TESTING

STEP 0: OPEN UP THE PYTHON EDITOR. If python is on your computer already, it's time to get started. On Windows, find IDLE in the start menu.

Week 2 Practical Objects and Turtles

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

1001ICT Introduction To Programming Lecture Notes

CS 51 Intro to CS. Art Lee. September 2, 2014

Python Programming: An Introduction to Computer Science. John M. Zelle, Ph.D.

Today s Topics... Intro to Computer Science (cont.) Python exercise. Reading Assignment. Ch 1: 1.5 (through 1.5.2) Lecture Notes CPSC 121 (Fall 2011)

6.170 Tutorial 3 - Ruby Basics

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Programming Languages

Anyone remember this old banner ad? (it was for Net Nanny Pornography? On *MY* Computer? It s more likely than you think. )

Introduction to Python

Introduction to Computers and Programming

CS 106 Introduction to Computer Science I

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Crash Dive into Python

PYTHON Basics

Chapter 14. Programming and Languages. McGraw-Hill/Irwin. Copyright 2008 by The McGraw-Hill Companies, Inc. All rights reserved.

Informatica e Sistemi in Tempo Reale

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Programming Languages & Tools

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Reference Guide Table of Contents

CSC230 Getting Starting in C. Tyler Bletsch

Domain Requirements in Spectralink SIP Phones

HOW TO USE SHUTTERFLY GALLERY

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

10 Java API, Exceptions, and Collections

Computer Programming Tutorial

Chapter 2 Writing Simple Programs

Hands-On Python A Tutorial Introduction for Beginners Python 3.1 Version

Code::Blocks Student Manual

Goal: Practice writing pseudocode and understand how pseudocode translates to real code.

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Hypercosm. Studio.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Introduction. What is an Operating System?

Decision Logic: if, if else, switch, Boolean conditions and variables

A software solution is needed for secure communication between you and a friend. Requirements

SparkLab May 2015 An Introduction to

Using Python in a High School Computer Science Program

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

How to Format a Bibliography or References List in the American University Thesis and Dissertation Template

NLP Lab Session Week 3 Bigram Frequencies and Mutual Information Scores in NLTK September 16, 2015

Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

CS 164 Winter 2009 Term Project Writing an SMTP server and an SMTP client (Receiver-SMTP and Sender-SMTP) Due & Demo Date (Friday, March 13th)

CMSC Fundamentals of Computer Programming II (C++)

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

How to use the Eclipse IDE for Java Application Development

Enterprise Messaging, Basic Voice Mail, and Embedded Voice Mail Card

University of Pennsylvania Department of Electrical and Systems Engineering Digital Audio Basics

CS 2302 Data Structures Spring 2015

Attack Lab: Attacks on TCP/IP Protocols

COMSC 100 Programming Exercises, For SP15

Python Lists and Loops

Computational Mathematics with Python

Sources: On the Web: Slides will be available on:

How to Write a Simple Makefile

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

Programming Languages

Assignment 4 CPSC 217 L02 Purpose. Important Note. Data visualization

vmprof Documentation Release 0.1 Maciej Fijalkowski, Antonio Cuni, Sebastian Pawlus

Transcription:

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie "Computers are good at following instructions, but not at reading your mind." -- D. Knuth Program: n. A magic spell cast over a computer allowing it to turn one's input into error messages. tr. v. To engage in a pastime similar to banging one's head against a wall, but with fewer opportunities for reward.

Useful Definitions Programming Language: A language that is designed to express instructions that can be carried out by a computer Program: A detailed set of instructions written in a programming language for a computer to perform Program Execution ("running" a program): The computer performs the program's instructions Machine language: The low-level language of a computer that consists of each computation's encoding as a sequence of 0s and 1s. In machine language, a program could look like this: 0110111100001010 1111011110101111 0000111101010111... A sequence of 0s and 1s (called bits) corresponds to operations such as Add, Store, Get Instruction, etc. Each computer (CPU) has its own machine language.

What is Python? A high-level language developed by Guido van Rossum in the Netherlands in the late 1980s. Released in 1991. Named after Monty Python Twice received recognition as the language with the largest growth in popularity for the year (2007, 2010) Clean, concise syntax o Easy to learn o Can create powerful programs quickly Design is compact o Language constructs are easy to learn o Easy to remember

How Does Python Work? Python is interpreted: o Each command is translated to machine language and executed on the fly The other type is compiled: o All commands are translated into machine language o The resulting program is stored in a new file o Then the machine language program is executed

How Python Works Python uses an interpreter, which is a program that translates each command in your program into machine language and executes it. Here's what you do: o Type your source code (Python) in a file (that ends in extension.py). o Invoke the Python interpreter (either from the command line or using IDLE) to translate your instructions into machine language and execute them. o Or you may also use Python interactively, and enter one command at a time: piglet.cs.utexas.edu:eberlein> python Python 2.5.4 (r254:67916, Jan 16 2009, 16:27:43) [GCC 4.2.2] on linux2 Type "help", "copyright", "credits" or "license" for more information >>>> print "welcome to cs 303e" welcome to cs 303e >>>> for i in range(3):... print i... 0 1 2 >>>>

Comments in a Python Program Explanations of code purpose in English Indicated by the # character Ignored by the interpreter The style guide for this class requires comments: o At the beginning of a module o At the beginning of a function o Before a chunk of related statements that carry out some unified action Example: # python program example def main(): # Welcome everyone to CS 303e print "Welcome to CS 303e!" Question: What output do you expect from the above Python program? A. Welcome to CS 303e! B. "Welcome to CS 303e!" C. welcome everyone to CS 303e

Repetition in Python In programs, we may want the computer to repeat an instruction or set of instructions more than once To do that, we use loops, also known as repetition structures Examples with a Loop: for i in [1, 2, 3]: print i # indentation is important! Output: 1 2 3 for i in range(3): print i Output: 0 1 2

Decisions in Python We may want the computer to perform certain instructions only under certain conditions. Test for the condition (to see if it is true) If condition is met, perform the action If condition not met, skip the action Example: i = 5 if i < 10: print i, "is less than 10"

Expected Output def main(): for i in [2, 4, 6]: print i What is the output? A. 1 B. 2 C. i 2 4 i 3 6 i

Python Program Structure A source file my contain function definitions, which are a collection of Python statements that will be executed together when the function is invoked. Function definitions start like this: def myfunctionname(): Statements that are part of the function (or a loop body) must be indented (say 4 spaces) underneath the above line - this indentation tells the Python interpreter which statements are part of your function. Functions are not executed until you call them. It is common to include a function called main which is executed first. A Python program can contain statements which are not part of any function. A comment is included after a # sign and is ignored by the python interpreter.

An Example: Some Source Code in Python def main(): # definition of main function print "hello" print "world" print "hello", "world" mynum = 13 print mynum*mynum # call the main function main() Output: 169 hello world hello world

Python Tour: Things to Remember Whitespace (blank spaces) matters o Statements inside functions/loops/conditionals must be indented Tells interpreter which statements belong inside Two spaces too few, six probably too many be consistent IDLE indents automatically Common to include the main function

Announcements Apply for your lab account today (if you have not already) use the link on the CS 303e website called "Lab Accounts". This week: no discussion section meetings on Friday Next week only: discussion sections meet in PAI 5.38 on Friday (at your regular time) o Your TA will be there and available to help with project 1 o TA will be at a computer with a sign introduce yourself, ask for help o If you have already finished project 1, you can skip discussion section (next week only)