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

Similar documents
Introduction to Python

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.

Welcome to Introduction to programming in Python

Introduction to Python

Chapter 2 Writing Simple Programs

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Crash Dive into Python

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

River Dell Regional School District. Computer Programming with Python Curriculum

Outline Basic concepts of Python language

Exercise 1: Python Language Basics

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

Informatica e Sistemi in Tempo Reale

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

Introduction to Python

Crash Dive into Python

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Invent Your Own Computer Games with Python

Computer Science for San Francisco Youth

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

CSC 221: Computer Programming I. Fall 2011

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

Computer Programming Tutorial

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

CS 106 Introduction to Computer Science I

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Pemrograman Dasar. Basic Elements Of Java

Chapter 2: Elements of Java

Practical Programming, 2nd Edition

Introduction to Python

Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Introduction to Java

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

Beginning to Program Python

Exercise 4 Learning Python language fundamentals

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

CS 1133, LAB 2: FUNCTIONS AND TESTING

Number Representation

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

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Tokens and Python s Lexical Structure

Writing Simple Programs

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

6.170 Tutorial 3 - Ruby Basics

An introduction to Python for absolute beginners

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

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

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

Computer Science 1 CSci 1100 Lecture 3 Python Functions

Introduction to Python for Text Analysis

Computational Mathematics with Python

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Java Crash Course Part I

Government Girls Polytechnic, Bilaspur

1001ICT Introduction To Programming Lecture Notes

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Udacity cs101: Building a Search Engine. Extracting a Link

Computational Mathematics with Python

Python Lists and Loops

PHP Tutorial From beginner to master

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Python Loops and String Manipulation

Financial Accounting Tutorial

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

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)

Introduction to Computers and Programming

NLP Programming Tutorial 0 - Programming Basics

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Chapter One Introduction to Programming

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science

Programming a mathematical formula. INF1100 Lectures, Chapter 1: Computing with Formulas. How to write and run the program.

Introduction to Visual C++.NET Programming. Using.NET Environment

Python 3 Programming. OCR GCSE Computing

ESPResSo Summer School 2012

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Solving Quadratic Equations by Factoring

Getting started with the Stata

Computer Science 217

Moving from CS 61A Scheme to CS 61B Java

Introduction to the course, Eclipse and Python

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Windows PowerShell Essentials

Introduction to. Marty Stepp University of Washington

Computer Programming I & II*

Homework 2. A 4*4 image with 16 pixels Borders unaltered. Color of B2 = Average color of (B1,A2,B3,C2) A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4

Variables, Constants, and Data Types

Programming Languages CIS 443

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

Computational Mathematics with Python

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Transcription:

CSCE 110 Programming Basics of Python: Variables, Expressions, and nput/output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Fall 2011

Python Python was developed in 1989 by Guido van Rossum in the Netherlands. Python was released for public distribution in early 1991. How did Python begin? van Rossum was having a hard time getting the job done with the existing tools available. He envisioned that there was an easier way to get things done. While Python has been around for over 15 years, Python is still relatively new to general software development. However, it has a lot of a lot of support from the community and new users are finding themselves programming in Python (and loving it) everyday.

Be an Explorer Since you are learning a new language, it is very important that you experiment with the material. Be curious about trying different things. Don t be afraid to make a mistake or crash your computer. That s how we learn best.

Python Distribution Used in this Course n my opinion, the easiest way to get Python (and the most popular modules) is to use the Enthought Python Distribution (EPD). t s free for academic use! t s available for the major operating systems: Windows, Mac, and Linux t s what s installed on the lab machines. Here s the URL if you want to install it on your personal computer. http://www.enthought.com/products/edudownload.php At the time of this writing, EPD uses version 2.7 of Python. We will not cover any nuances associated with Python 3.x in this course.

WingWare DE 101 Now that we have a Python distribution, we need a way to type in our code. We will use WingWare DE 101 in this course. t s free for academic use! t s available for the major operating systems: Windows, Mac, and Linux t s what s installed on the lab machines. 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! We will take it slow and easy in the beginning. want you to get comfortable getting acquainted with the basics. Following along in class is not enough. You MUST type in the programs on your computer and see how they work. You MUST type in the programs so that if you make a mistake you can learn how to make the appropriate correction. You MUST type in the programs because programming promotes active (and not passive) learning.

nteractive Execution in the Python Shell

Hello, World! Python s print statement is the tool for displaying program output to your users. Type the following in the command-line editor. > > > print Hello, World!

Using Python as a Calculator > > > 3 + 4 > > > 15 / 3 > > > 12 * 10 + 4

Operators Mathematical operators: +, -, *, /, %, ** addition (+) subtraction (-) multiplication (*) division (/) modulus or remainder (%) exponenentiation (**) Comparision operators: <, <=, >, >=, ==,!= strictly less than (<) less than or equal to (<=) strictly greater than (>) greater than or equal to (>=) equal to (==) not equal to (!=) Expression conjunctive operators: and, or, not

Variables and Assignment > > > counter = 0 > > > miles = 1000.0 > > > name = Bob > > > counter = counter + 1 > > > kilometers = 1.609 * miles > > > print miles 1000.0 > > > print counter 1 > > > print kilometers 1609.0

Variables Variables are the set of valid strings that are allowed as names in a computer language such as Python. The rules for forming Python variables are: First character must be a letter or underscore (_) Any additional characters can be alphanumeric or underscore Case-sensitive No variables can begin with a number. No symbols other than alphanumerics or underscores are ever allowed. No variable can be the same as keywords, which form the foundation of the language.

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

Numbers Python supports five basic numerical types. int (signed integers) long (long integers) bool (Boolean values) float (floating point real numbers) complex (complex numbers) Here are some examples. int: 100 200-437 long: -84140l 299556678883L bool: True False float: 3.456-33.55 34.1 complex: 6.23+1.5j -1.23-875J 0+1j

Strings Strings are a contiguous set of characters in between quotation marks. Strings are immutable sequences. A program can refer to elements or subsequences of strings. However, strings cannot be modified in place. Python allows for either pairs of single or double quotes. Triple quotes (three consecutive single or double quotes) can be used to escape special characters. Subsets of strings can be taken using the index ( [ ] )and slice ( [ : ] )operators,whichworkwithindexesstarting at 0 in the beginning of the string and working their way from -1 at the end. Slice ( [] ) gives the character from the given index or location Range slice ( [x:y] ) gives the characters starting at index x and ending at index y -1 The plus ( + )signisthestringconcatenationoperator. The asterisk ( * )istherepetitionoperator.

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 = t\ s a girl! > > > string "t s a girl!"

Writing Python Programs

User nput 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 as hello.py. Afterward, run the program. 3. Discuss the difference between the two different approaches for entering Python programs.

Write the Following Python Programs 1. Full name greeting. Write a program that asks for a person s first name, then middle, and then last. Afterwards, it should greet the person using their full name. 2. Guessing game? Write a program that asks a person to guess anumber.add1totheguessednumber,andthensuggest that their guess was close to being correct. 3. Guessing game (version 2)? Write a program that asks a person to guess a number. Tell the user that their guess was off by a random amount.