csce 110 — programming i basics of python: variables...

Post on 05-Jun-2018

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSCE 110 — Programming IBasics of Python: Variables, Expressions, and

Input/Output

Dr. Tiffani L. Williams

Department of Computer Science and Engineering

Texas A&M University

Fall 2011

Python

I Python was developed in 1989 by Guido van Rossum in theNetherlands.

I Python was released for public distribution in early 1991.I How did Python begin?

I van Rossum was having a hard time getting the job done withthe existing tools available.

I He envisioned that there was an easier way to get things done.

I While Python has been around for over 15 years, Python is stillrelatively new to general software development. However, ithas a lot of a lot of support from the community and newusers are finding themselves programming in Python (andloving it) everyday.

Be an Explorer

I Since you are learning a new language, it is very importantthat you experiment with the material.

I Be curious about trying different things.I Don’t be afraid to make a mistake or crash your computer.

That’s how we learn best.

Python Distribution Used in this Course

I In my opinion, the easiest way to get Python (and the mostpopular modules) is to use the Enthought Python Distribution(EPD).

I It’s free for academic use!I It’s available for the major operating systems: Windows, Mac,

and LinuxI It’s what’s installed on the lab machines.I Here’s the URL if you want to install it on your personal

computer.http://www.enthought.com/products/edudownload.php

I At the time of this writing, EPD uses version 2.7 of Python.We will not cover any nuances associated with Python 3.x inthis course.

WingWare IDE 101

I Now that we have a Python distribution, we need a way totype in our code.

I We will use WingWare IDE 101 in this course.I It’s free for academic use!I It’s available for the major operating systems: Windows, Mac,

and LinuxI It’s what’s installed on the lab machines.I Here’s the URL if you want to install it on your personal

computer.http://wingware.com/downloads/wingide-101/4.0.3-1/

binaries

Now, we are ready to start programming!

I We will take it slow and easy in the beginning. I want you toget comfortable getting acquainted with the basics.

I Following along in class is not enough.I You MUST type in the programs on your computer and see

how they work.I You MUST type in the programs so that if you make a mistake

you can learn how to make the appropriate correction.I You MUST type in the programs because programming

promotes active (and not passive) learning.

Interactive Execution in the

Python Shell

“Hello, World!”

I Python’s print statement is the tool for displaying programoutput to your users.

I Type the following in the command-line editor.>‌>‌> print ’Hello, World!’

Using Python as a Calculator

>‌>‌> 3 + 4>‌>‌> 15 / 3>‌>‌> 12 * 10 + 4

Operators

I Mathematical operators: +, -, *, /, %, **I addition (+)I subtraction (-)I multiplication (*)I division (/)I modulus or remainder (%)I exponenentiation (**)

I Comparision operators: <, <=, >, >=, ==, !=I strictly less than (<)I less than or equal to (<=)I strictly greater than (>)I greater than or equal to (>=)I equal to (==)I not equal to (!=)

I Expression conjunctive operators: and, or, not

Variables and Assignment

>‌>‌> counter = 0>‌>‌> miles = 1000.0>‌>‌> name = ’Bob’>‌>‌> counter = counter + 1>‌>‌> kilometers = 1.609 * miles>‌>‌> print miles1000.0>‌>‌> print counter1>‌>‌> print kilometers1609.0

Variables

I Variables are the set of valid strings that are allowed as namesin a computer language such as Python.

I The rules for forming Python variables are:I First character must be a letter or underscore (_)I Any additional characters can be alphanumeric or underscoreI Case-sensitive

I No variables can begin with a number.I No symbols other than alphanumerics or underscores are ever

allowed.I No variable can be the same as keywords, which form the

foundation of the language.

Keywords

and as assert breakclass continue def delelif else except exec

finally for from globalif import in is

lambda not or passprint raise return trywhile with yield

Numbers

I Python supports five basic numerical types.I

int (signed integers)I

long (long integers)I

bool (Boolean values)I

float (floating point real numbers)I

complex (complex numbers)

I Here are some examples.I int: 100 200 -437I long: -84140l 299556678883LI bool: True FalseI float: 3.456 -33.55 34.1I complex: 6.23+1.5j -1.23-875J 0+1j

StringsI Strings are a contiguous set of characters in between quotation

marks.I Strings are immutable sequences.I A program can refer to elements or subsequences of strings.

However, strings cannot be modified in place.

I Python allows for either pairs of single or double quotes. Triplequotes (three consecutive single or double quotes) can be usedto escape special characters.

I Subsets of strings can be taken using the index ( [ ] ) andslice ( [ : ] ) operators, which work with indexes startingat 0 in the beginning of the string and working their way from-1 at the end.

I Slice ( [] ) gives the character from the given index or locationI Range slice ( [x:y] ) gives the characters starting at index x

and ending at index y -1

I The plus ( + ) sign is the string concatenation operator.I The asterisk ( * ) is the repetition operator.

String Examples

>‌>‌> string = "Texas A&M University">‌>‌> string[0]’T’>‌>‌> string[2:5]’xas’>‌>‌> string[4:]’s A&M University’>‌>‌> string[:6]’Texas ’>‌>‌> string * 2’Texas A&M UniversityTexas A&M University’>‌>‌> string = ’It\’s a girl!’>‌>‌> string"It’s a girl!"

Writing Python Programs

User Input

1. Type the following text directly into your editor.>‌>‌> name = raw_input("Please enter your name: ")>‌>‌> print "Hello", name, "- good to see you!"

2. Type the above text into a file called hello.py and save it ashello.py. Afterward, run the program.

3. Discuss the difference between the two different approaches forentering Python programs.

Write the Following Python Programs

1. Full name greeting. Write a program that asks for aperson’s first name, then middle, and then last. Afterwards, itshould greet the person using their full name.

2. Guessing game? Write a program that asks a person to guessa number. Add 1 to the guessed number, and then suggestthat their guess was close to being correct.

3. Guessing game (version 2)? Write a program that asks aperson to guess a number. Tell the user that their guess wasoff by a random amount.

top related