CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

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

Functions and Decomposition

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

Python Input, output and variables

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

CMPT 120 Introduction To Computing Science And Programming I. Pseudocode. Summer 2012 Instructor: Hassan Khosravi

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

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

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Lecture 3. Input, Output and Data Types

Python Class-Lesson1 Instructor: Yao

Variables, Expressions, and Statements

Topic 2: Introduction to Programming

CS 112: Intro to Comp Prog

Fundamentals: Expressions and Assignment

Python for Non-programmers

Algorithms and Programming I. Lecture#12 Spring 2015

Getting Started Values, Expressions, and Statements CS GMU

Chapter 2 Getting Started with Python

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

Expressions and Casting

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

Programming for Engineers Introduction to C

Introduction to Python Code Quality

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

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

CS 115 Lecture 4. More Python; testing software. Neil Moore

Variables, expressions and statements

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

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

1/11/2010 Topic 2: Introduction to Programming 1 1

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

More about Binary 9/6/2016

The current topic: Python. Announcements. Python. Python

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

Lists, loops and decisions

Part III Appendices 165

MEIN 50010: Python Introduction

Introduction to: Computers & Programming: Review prior to 1 st Midterm

T H E I N T E R A C T I V E S H E L L

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Chapter 17. Fundamental Concepts Expressed in JavaScript

CSE 115. Introduction to Computer Science I

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

Visual C# Instructor s Manual Table of Contents

Lecture 4. Defining Functions

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Welcome to Python 3. Some history

ENGR (Socolofsky) Week 02 Python scripts

Expressions and Variables

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

ENGR 101 Engineering Design Workshop

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Programming with Python

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

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Lecture 4: Basic I/O

Fundamentals of Programming Session 4

Notes on Chapter 1 Variables and String

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Not-So-Mini-Lecture 6. Modules & Scripts

Math Day 2 Programming: How to make computers do math for you

Built-in Types of Data

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

AP Computer Science A

Introduction to Python

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

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

Full file at

Introduction to programming with Python

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Introduction to computers and Python. Matthieu Choplin

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

CS 112: Intro to Comp Prog

Jython. secondary. memory

Introduction to Scratch

Python Programming Exercises 1

Objectives. In this chapter, you will:

A variable is a name for a location in memory A variable must be declared

Advanced Python. Executive Summary, Session 1

The Big Python Guide

Introduction to Python

Exercise: Inventing Language

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

ECE 122 Engineering Problem Solving with Java

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Invent Your Own Computer Games with Python

Stored (and reused) Steps

Transcription:

CMPT 120 Basics of Python Summer 2012 Instructor: Hassan Khosravi

Python A simple programming language to implement your ideas Design philosophy emphasizes code readability Implementation of Python was started in 1989 by Guido van Rossum In this course we will be using the Python 2.7.3 version http://www.python.org/download/releases/2.7.3/ Python has an interactive interpreter. It will execute immediately. You can also type python code into a file and save it. 1.2

First program For some reason, when people are taught to program, the first program they see is one that prints the words Hello world on the screen. >>> print "Hello world" Hello world The Interpreter vs. the Editor Running hello world with both Any text in quotes, like "Hello world" in the example, is called a string. Characters are letters, numbers, spaces, and punctuation. Strings have to be placed in quotes to be distinguished from Python commands 1.3

Statement Statements are the basic building blocks of Python programs. Each statement expresses a part of the overall algorithm that you re implementing. The statements are executed in the order they appear in the file. So, the Python program print "Hello world!" print "I m a Python program that prints stuff." Hello world! I m a Python program that prints stuff. 1.4

Doing Calculations The Python operators +, -, *, and / perform addition, subtraction, multiplication, and division, as you might expect. >>> print 10-2 8 >>> print 15/3 5 >>> print 25+19*5 120 >>> print 10.2 / 2 / 2 2.55 The order is the same as mathematics operators You can () to change the order of operators print (76+100)/2 print 76+100/2 1.5

Calculations on strings >>> print "An" + "Expression" AnExpression >>> print "An " + Expression An Expression >>> print ABC * 4 ABCABCABCABC A number, or anything else in quotes, is treated like a string >>> print 120 * 3 360 >>> print "120" * 3 120120120 >>> print "120 * 3" 120 * 3 single quotes ( ) and double quotes (") can be used interchangeably. 1.6

Functions Python can also use functions as part of expressions. You give the function some arguments, and something is done to calculate the result >>> print round(13.89) 14.0 >>> print round(-4.3) -4.0 >>> print round(1000.5) 1001.0 1.7

Functions Functions can take any type of information as their argument and can return any type. print len("hello") 5 >>> print len("-<()>-") 6 >>> print len("") 0 1.8

Storing Information Sometimes, you need to perform a calculation to be used later, without needing to display the results right away. Whenever we need the computer to temporarily remember some information in a program, we will use a variable. Average = (10 +20)/2 >>> num = 7 >>> word = "yes" >>> print num - 3 4 >>> print word + word yesyes >>> num = 4 >>> print num 3 1 1.9

Types Python treats numbers (like 2, -10, and 3.14) differently than strings print 10/2 5 >>> print "abc" / 2 TypeError: unsupported operand type(s) for /: str and int TypeError indicates that you ve used values whose types can t be used with the given operation. + operator does different things on numbers (addition) and strings (joining) >>> print 10/2 5 >>> print 10/3 3 >>> print 10.0/3 3.33333333333 1.10

Type Conversion There is a built-in function called type type(10/3) type(10.0/3) There are Python functions that can be used to change a value from one type to another. int() converts to an integer float() converts to a floating point value str() converts to a string. 1.11

Example float(10) 10.0 >>> str(10) 10 >>> int( 10 ) 10 >>> int(83.7) 83 >>> str(123.321) 123.321 >>> int("uhoh") ValueError: invalid literal for int(): uhoh 1.12

>>> total = 46 >>> num = 10 >>> print total/num 4 >>> print float(total)/num 4.6 >>> print float(total/num) 4.0 1.13

You can print out multiple values with the comma, but they are separated by spaces: >>> print "The sum was", total, "." The sum was 46. Note that there s a space between the 46 and the period. You can remove this by combining strings to get the result we want: >>> print "The sum was " + total + "." TypeError: cannot concatenate str and int objects >>> print "The sum was " + str(total) + "." The sum was 46. 1.14

User Input To do this in Python, use the raw_input function. This function will give the user whatever message you tell it to, wait for them to type a response and press enter, and return their response to your expression. name = raw_input("what is your name? ") print "Hello, " + name + ". " If you want to treat the user s input as an integer or floating point number, you have to use one of the type conversion m = float(raw_input("enter your height (in metres): ")) inches = 39.37 * m print "You are " + str(inches) + " inches tall." 1.15

Example problem solving feet and inchs write Enter your height (in metres): read metres set totalinches to 39.37 metres set feet to totalinches/12 set inches to totalinches feet 12 round inches to the nearest integer write You are feet inches tall. metres = float(raw_input("enter your height (in metres): ")) total_inches = 39.37 * metres feet = int(total_inches/12) inches = total_inches - feet*12 inches = int(round(total_inches - feet*12)) print "You are " + str(feet) + " feet and " + str(inches) + " inches tall." 1.16

Printing single and double quotes How can we print out single quote or double quote Print Hi Print Hi What if you want Hi to be the output? Put a backslash before the quote. print " \"hi\" " Use a single quote to wrap up the string. print ' "Hi" ' print " 'Hi' Use s triple-quoted string print """ "Hi" """ print """ 'Hi' """ 1.17

Reading Read Chapter 2 from Introduction to Computing Science and Programming I Read Chapter 2 in How to Think Like a Computer Scientist 1.18