CSCE 110 Programming I

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

Variable and Data Type I

ENGR 101 Engineering Design Workshop

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Variable and Data Type I

Jython. secondary. memory

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Variables, expressions and statements

PROGRAMMING FUNDAMENTALS

Chapter 2 Writing Simple Programs

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Basic Syntax - First Program 1

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Program Planning, Data Comparisons, Strings

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

And Parallelism. Parallelism in Prolog. OR Parallelism

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Getting Started with Python

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Fundamentals of Programming (Python) Getting Started with Programming

Some material adapted from Upenn cmpe391 slides and other sources

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Python Input, output and variables

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Programming with Python

Table of Contents EVALUATION COPY

Short, Unique and Mysterious

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

UNIVERSITÀ DI PADOVA. < 2014 March >

Computational Integer Programming. Lecture 4: Python. Dr. Ted Ralphs

Conditional Expressions and Decision Statements

Decisions, Decisions. Testing, testing C H A P T E R 7

Python Class-Lesson1 Instructor: Yao

Text Input and Conditionals

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

REVIEW. The C++ Programming Language. CS 151 Review #2

CMSC 201 Computer Science I for Majors

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

CSCE 110: Programming I

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

Python lab session 1

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

About Variables in Python F E B 1 1 T H

The current topic: Python. Announcements. Python. Python

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Ex: If you use a program to record sales, you will want to remember data:

MEIN 50010: Python Introduction

Senthil Kumaran S

D R S H YA M N C H AW D A

Python Games. Session 1 By Declan Fox

ENGR 102 Engineering Lab I - Computation

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

SCoLang - Language Reference Manual

Introduction to Python, Cplex and Gurobi

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Introduction to Python - Part I CNV Lab

The Big Python Guide

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Hello, World and Variables

Variables, Expressions, and Statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSCE 110 Programming I

Chapter 2 Getting Started with Python

Introduction to Python

Beyond Blocks: Python Session #1

CSC 221: Introduction to Programming. Fall 2013

>>> * *(25**0.16) *10*(25**0.16)

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Python - Variable Types. John R. Woodward

Python Unit

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Python for ArcGIS. Lab 1.

Chapter 2: Introduction to C++

Lecture 3. Input, Output and Data Types

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

Part III Appendices 165

BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to programming with Python

6.096 Introduction to C++ January (IAP) 2009

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

CS1 Lecture 3 Jan. 18, 2019

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Transcription:

CSCE 110 Programming I Basics of Python (Part 1): Variables, Expressions, and Input/Output Dr. Tiffani L. Williams Department of Computer Science and Engineering Texas A&M University Spring 2013 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 1 / 19

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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 2 / 19

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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 3 / 19

Python Distribution Used in this Course In my opinion, the easiest way to get Python (and the most popular modules) is to use the Enthought Python Distribution (EPD). It s free for academic use! It s available for the major operating systems: Windows, Mac, and Linux It 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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 4 / 19

WingWare IDE 101 Now that we have a Python distribution, we need a way to type in our code. We will use WingWare IDE 101 in this course. It s free for academic use! It s available for the major operating systems: Windows, Mac, and Linux It 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.1.10-1/binaries Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 5 / 19

Now, we are ready to start programming! We will take it slow and easy in the beginning. I 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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 6 / 19

Interactive Execution in the Python Shell Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 7 / 19

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! Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 8 / 19

Using Python as a Calculator > > > 3 + 4 > > > 15 / 3 > > > 12 * 10 + 4 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 9 / 19

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 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 10 / 19

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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 11 / 19

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 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 12 / 19

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 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 13 / 19

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 Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 14 / 19

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, which work with indexes starting 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 ( + ) sign is the string concatenation operator. The asterisk ( * ) is the repetition operator. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 15 / 19

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!" Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 16 / 19

Writing Python Programs Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 17 / 19

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 as hello.py. Afterward, run the program. 3 Discuss the difference between the two different approaches for entering Python programs. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 18 / 19

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 a number. Add 1 to the guessed number, and then suggest 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. Tiffani L. Williams (Texas A&M) CSCE 110: Basics of Python (Part 1) Spring 2013 19 / 19