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

Similar documents
Python Input, output and variables

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

Python 2 Conditionals and loops Matthew Egbert CS111

Lecture 1. basic Python programs, defining functions

Variable and Data Type I

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Algorithms and Programming I. Lecture#12 Spring 2015

Python 2 Conditionals and loops

Chapter 2 Getting Started with Python

Introduction to computers and Python. Matthieu Choplin

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

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

Variable and Data Type I

Variables, expressions and statements

Visual C# Instructor s Manual Table of Contents

CSC 221: Introduction to Programming. Fall 2013

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

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

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

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

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

Introduction to Python, Cplex and Gurobi

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

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

Full file at

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Variables, Constants, and Data Types

The current topic: Python. Announcements. Python. Python

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

Topic 2: Introduction to Programming

Introduction to Python

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

Special Section: Building Your Own Compiler

Key Differences Between Python and Java

Welcome to Python 3. Some history

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

Chapter 2 Working with Data Types and Operators

Language Reference Manual

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Final Labs and Tutors

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 1. Types, Expressions, & Variables

Jython. secondary. memory

Short, Unique and Mysterious

Program Fundamentals

Level 3 Computing Year 2 Lecturer: Phil Smith

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

Fundamentals of Programming CS-110. Lecture 2

Introduction to Programming EC-105. Lecture 2

Full file at

ENGR 101 Engineering Design Workshop

Introduction to programming with Python

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Datatypes, Variables, and Operations

printf( Please enter another number: ); scanf( %d, &num2);

C H A P T E R 1. Introduction to Computers and Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

MACHINE LEVEL REPRESENTATION OF DATA

In this chapter you ll learn:

Operations. Making Things Happen

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

Introduction to Python

Unit 7: Algorithms and Python CS 101, Fall 2018

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

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

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Basics of Java Programming

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Lecture 3. Input, Output and Data Types

Declaration and Memory

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

CSCE 110 Programming I

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

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

Introduction to Programming

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

CMSC 201 Computer Science I for Majors

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

C++ Programming: From Problem Analysis to Program Design, Third Edition

AP Computer Science A

JME Language Reference Manual

Reserved Words and Identifiers

Variables and data types

CSI33 Data Structures

VBScript: Math Functions

Transcription:

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

Today s lecture What is Python? Displaying text on screen using print() Variables Numbers and basic arithmetic Getting input from keyboard using input() 2

What is a programming language? A formal language that specifies how to perform a computational task Many programming languages exist: Visual Basic C and C++ C# Java Python Python was created in 1989 by Guido Van Rossum in The Netherlands 3

Statements A program consists of a series of commands called statements They are generally executed (ie. run) in the order they appear The statements must be written correctly otherwise you will get a syntax error Python programs are saved in files with the.py extension INTER PRETE R 1001 0011 1001 0100 0100 1110 1010 1011 1111 0000 0101 0000 1010 0000 1011 0101 0101 0101 0100 0010 0000 1010 1000 1000 1111 1100 1001 0010 1010 1010 0100 0001 0100 1100 1010 0000 1001 0100 1001 0001 0010 0010 0000 1010 1010 1010 0100 0100 1001 0110 0100 1110 0001 0101 4

Translating code The statements in our programs are translated into simpler instructions that the CPU can execute Two ways of doing this: Compiler: translates the entire program file at once Interpreter: repeatedly translates one line and runs it Python is an interpretative programming language There are also compilers available for Python 5

IDLE Integrated Development Environment (IDE) An IDE is used by programmers to: Write code Check for errors Translate code and run the program We use the IDLE IDE; a popular IDE for Python IDLE has a shell for the Python interpreter You can also create a new file that can be compiled when you ve finished writing a program 6

IDLE IDE The interpreter allows you to type statements, translate them and see them run instantly Very helpful for experimentation and learning 7

Interactive Interpreter Vs Running a script Interactive Interpreter Allows you to type statements directly at the prompt Statement is executed when you hit <Enter> Very useful for experimentation Good for learning Running a Script Type a sequence of statements into a file Save the file with the file extension.py Running the program executes each statement in turn 8

IDLE IDE Create a new program by clicking on File New File Type your statements in the file, then click on Run Run Module 9

Hello world Traditional first program is displaying Hello World on screen To display text on screen you use the print() function 10

Hello world Using the Python interpreter: 11

Printing output Use the print statement Code Output Printing more than one thing on a single line Separate each thing with a comma Single space used between different things in the output Code Output 12

Exercise 1 TRY IT OUT! What is the output produced by the following statements? 13

Comments When writing a program, it is helpful to leave comments in the code You can write a comment in Python by typing a # in front of the line The compiler will ignore all text after the # 14

Strings: Data types Sequence of characters Plain text (ASCII or Unicode) Enclosed in quote marks Eg: "Hello", "Goodbye" Integers: Whole numbers (ie. without a decimal point) Eg. -100, 0, 45 Floating point numbers: Numbers with a decimal point Eg. 5.2, -1.002, 0.0 15

Variables A container in the computer s memory in which you can store data A variable s value can change when the program runs Python variables are loosely-typed; they can hold any data type 16

Variables Rules to follow when naming your variables: Names should reflect what is stored in the variable Can begin with a letter or underscore (eg. _ ) Variable names can include numbers Generally, all words are lowercase and words are separated using an underscore 17

Assignment statement Assigning a value to a variable: 18

Assignment statement Changing the value in a variable: 19

Exercise 2 What is the output produced by the following statements? 20

Arithmetic operations Operation Symbol Example Exponent ** 2 ** 3 = 8 Multiply * 2 * 2 = 4 Divide / 10 / 3 = 3.333 Divide (integer) // 10 // 3 = 3 Remainder % 10 % 3 = 1 Add + 8 + 9 = 17 Subtract - 9-7 = 2 21

Print() function Used to display information on the screen Code Output 22

Print() function Concatenation: this involves joining two or more strings together Repetition: lets you print a string multiple times 23

Exercise 3 What is the output for the following code? 24

Getting input Primary source of input for our programs will be the keyboard The input() function: Prints a prompt for the user to read Captures the user s keystrokes When the user presses Enter, stores the string in a variable 25

Getting input Converting the string value returned by input() to an integer or floating point value You need to do this when you want the actual numerical value the user is entering age = int(input("enter your age: ")) height = float(input("enter your height: ")) height = height + 1.5 26

Exercise 4 Write a Python program that converts feet to meter. The conversion formula is: 1 foot = 0.3048 meters Your program s output should look like this: Enter feet: 34 34 feet is equal to 10.3632 meters You will need to use: Variables Arithmetic operator input() and print() Link: https://coderunner2.auckland.ac.nz/moodle/mod/quiz/view.php?id= 629 27

Algorithm Prompt for the value Create a variable and set the value (feet_to_metres = 0.3048) Calculate the corresponding value print the result 28

Summary Python programs consist of statements that are translated by an interpreter or compiler into instructions that the CPU can execute We ve discussed the Python programming language and its features: print() Data types: string, int, float Arithmetic operators Variables and variable naming conventions input() and int(), float() Post-Lecture-Quiz: PLQ_23 https://coderunner2.auckland.ac.nz/moodle/mod/quiz/view.php?id=6 30 29