Algorithms and Data Structures

Similar documents
Algorithms and Data Structures

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

Algorithms and Data Structures

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

Variable and Data Type I

Variables, expressions and statements

PROGRAMMING FUNDAMENTALS

Variable and Data Type I

ENGR 101 Engineering Design Workshop

Algorithms and Data Structures

Short, Unique and Mysterious

Chapter 2 Writing Simple Programs

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

Algorithms and Programming I. Lecture#12 Spring 2015

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Python Programming Exercises 1

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

The float type and more on variables FEB 6 TH 2012

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

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

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

PYTHON. Values and Variables

Jython. secondary. memory

Programming with Python

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

Variables and Constants

Getting Started with Python

Fundamentals of Programming (Python) Getting Started with Programming

Basic Syntax - First Program 1

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

Variables, Expressions, and Statements

CMSC 201 Computer Science I for Majors

And Parallelism. Parallelism in Prolog. OR Parallelism

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

CSI Lab 02. Tuesday, January 21st

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

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

Learning the Language - V

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

VARIABLES & ASSIGNMENTS

About Variables in Python F E B 1 1 T H

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

CSCE 110 Programming I

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Fundamentals: Expressions and Assignment

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSCE 120: Learning To Code

Eng. Mohammed S. Abdualal

Chapter 2 Getting Started with Python

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

egrapher Language Reference Manual

Introduction to Programming in Turing. Input, Output, and Variables

Arithmetic Expressions in C

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

OA: Operations and Algebraic Thinking

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

ENGR 102 Engineering Lab I - Computation

CS 2316 Individual Homework 1 Python Practice Due: Wednesday, January 20 th, before 11:55 PM Out of 100 points

9. Elementary Algebraic and Transcendental Scalar Functions

6th Grade Arithmetic (with QuickTables)

Lecture 3. Input, Output and Data Types

Math Content

Introduction to Python Programming

SIMPLE I/O WITH PYTHON

The C++ Language. Arizona State University 1

UNIT II DATA, EXPRESSIONS, STATEMENTS. Python being a interpreter language has two modes namely: Interactive mode, Script Mode or Normal mode

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

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

2 nd Week Lecture Notes

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

What Version Number to Install

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Introduction to Programming

Topic 2: Introduction to Programming

Section we will not cover section 2.11 feel free to read it on your own

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

Information for Parents/Carers. Mathematics Targets - A Year 1 Mathematician

Getting started 7. Saving data 23

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Python Input, output and variables

Lesson 3: Basic Programming Concepts

Introduction to Python

Lecture 3 Tao Wang 1

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

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

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

Text Input and Conditionals

DaMPL. Language Reference Manual. Henrique Grando

Mathematical Data Operators

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

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

Variables and Typing

Transcription:

Algorithms and Data Structures 3. Program Components and Data Types Łódź 2012

Exercise Body Mass Index - Type in the program - Save it as bmi.py - Run the script >>> Enter your name: John >>> Enter your height [in cm]: 185 >>> Enter the mass of your body [in kg]: 87 2

Variables - Variables are names that refer to data stored in the memory. - Variables are also named identifiers in Python. - There are 5 variables used in program BMI.py: YourName, var, YourHeight, YourMass, BMI 3

Variable names Variable names - may consist of lower or upper alphabetic characters, the underscore _, and the digits 0-9, - can not start with a digit, - can not be Python keywords and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is def for lambda try return To make the program easier to follow, define meaningful identifiers. 4

Program statements - Program is a sequence of statements. - Two types of statements are used in bmi.py program: assignment statement and print() statement. Assignment statements: <variable> = <expression> Read assignment statement from right to left: 1) Evaluate the expression on the right. 2) Assign the variable on the left to refer to that value. >>> #Computes the right side to be -2 and assigns x to refer to -2 >>> x = 10 17 + 5 An assignment statement = is not like a math equals sign. It is rather a delimiter to separate the variable from expression. 5

Program statements Print statements: print <expression1>, <expression2>, are used to produce program output. >>> #Expression in quotation marks (so-called strings) are printed literally. >>> print Ala ma kota. >>> print Ala ma kota. >>> #Expressions without quotation marks have their value printed. >>> n = 5 >>> print Ala ma kota oraz, n-3, psy. - Expressions are separated by single spaces. - Each print statement produces output in a separate line. 6

- Type in and run the following script Program statements - Change the print() statement to >>> print The area of a circle with radius r is area Explain the result. - Technically, print() is a built-in function that is called to incorporate a print statement into a program. 7

Data types - A variable in Python may refer to any type of data. Three examples of data types are strings, integers and floats. Strings are sequences of characters inside single or double quotation marks. >>> Motto = Programming is fun! >>> aquestion = Does she like studying at IFE? Integers are whole number values. >>> Large_Score = 857 >>> NegativeResult = -39 Floats (floating point) are numbers with a fractional part. >>> DistanceInMeters = 857.21 >>> SmallQuantity = -0.000138 8

Expressions Input expression input(prompt) is used to produce program intput. It displays prompt and returns user input as a string. The prompt is printed to alert the user that the program is waiting for input. >>> age = input ( Enter your age: ) >>> print When I was, age, strange thing happened!) 9

Expressions Numeric expressions use the arithmetic operations +, -, *, /, //, **, %, respectively for addition, subtraction, multiplication, division, integer division, raising to a power, and modulo (remainder from division of integer numbers). Rules of precedence are: 1) exponentiation, 2) multiplication, division and modulo (left to right), 3) addition and subtraction (left to right). Parentheses may be used to change the order of operations. - Integer division rounds down to the nearest integer >>> 7//2 >>> -1//3 - Integer division of floats rounds down to an integer, but result is of float type. - Variables must be assigned a value before being used in an expression. 10

Comments Comments begin with a number (hash) sign # and signify that whatever follows is to be ignored by the inpterpreter. # Text that helps explain your program to others Comments can be placed everywhere in a Python program. They explain the code for a human reader rather than for the interpreter. The Python interpreter translates the program code to the form that can be executed by the computer. When one starts the Python shell or PyLab, the interpreter displays a prompt sign (e.g. >>>). This means it is ready to translate any input entered after the promt. >>> print Hi, buddy! 11

Exercises 3.1. Give 3 names that are legal and 3 names that are not legal Python identifiers. 3.2. List each variable in the program below and give the type of data that is refered to. Identify the expressions and assignment statements 3.3. Explains what happens if the final space is deleted from the input statement >>> k = input ( Enter k: ) 12

Exercises 3.4. Determine the value of each of these Python expressions (a) 1 + 2 * 3 4 * 5 + 6 (b) 1 + 2 ** 3 * 4-5 (c) 1 / 2 3 / 4 (d) 1 // 2 3 //4 + 5 // 6 (e) 1 + 4 2 / 2 (f) (1 + 4 2) / 2 3.5. Determine the output of each of these code fragments (a) x = 10 (b) x = 10 (c) x = 10 y = 15 y = 15 y = 15 print x, y print x, y z = 20 y = x y = x x = z print x, y x = y z = y x = 5 print x, y y = x print x, y x = 5 print x, y, z print x, y 13

Exercises 3.6. Write a program average.py that calculates and prints the average of 3 integer numbers. Print the result as a floating-point number with two digits after the decimal separator. Modify the number of displayed fraction digits. Hint: Use a print statement similar to the one used in line 10 of BMI.py program. 3.7. Modify listing average.py to write a program rect.py that calculates and prints the area and perimeter of a rectangle given its length and width. Run your program with several different values of the length and width to test it. a b 3.8. Modify listing average.py to write a program cube.py that calculates and prints the volume and surface area of a cube given its width. Run your program with several different values of the width to find a point at which volume equals (numerically) surface area. a a 14 a

Exercises 3.9. Modify listing average.py to write a program sphere.py that calculates and prints the volume and surface area of a sphere given its radius. Run your program with different values of the radius to find the point at which volume equals (numerically) surface area. 3.10. Write a Python script to print a box like the one below. ********** * * ********** 3.11. Having a tip calculator in your mobile phone would be useful. Write one assuming there is a Python interpreter available on the phone. Ask the user for the price of the meal and the percent tip they want to leave. Then print both the tip amount and the total bill with the tip included. 15

Summary 1) Variables refer to 2) via assigment statements. Remember to read them from right to left. 3) Data can be of type string, integer or float (so far). The data type of a variable determines what one can do with it. 4) input() is an expression to enter data into a running program (in a Python console mode). 5) Arithmetic operations are used in numeric expressions. They follow rules of precedence. One can change the order of operations in an expression by using parentheses. 6) The = symbol denotes assignment, not mathematical equal sign. 7) Comments are ignored by the Python interpreter. A program script should be rich in comments. (Why?) 16

Literature Brian Heinold, Introduction to Programming Using Python, Mount St. Mary s University, 2012 (http://faculty.msmary.edu/heinold/python.html). Brad Dayley, Python Phrasebook: Essential Code and Commands, SAMS Publishing, 2007 (dostępne też tłumaczenie: B. Dayley, Python. Rozmówki, Helion, 2007). Mark J. Johnson, A Concise Introduction to Programming in Python, CRC Press, 2012. 17