Introduction to Programming

Similar documents
Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

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

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

FUNDAMENTAL DATA TYPES

Software and Programming 1

Chapter Two: Fundamental Data Types

Software and Programming 1

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

9/1/2015. Chapter 2 Using Objects. Objects and Classes. Using Objects. Using Objects. Using Objects. Classes. Ch. 1/2 Lab Time

Introduction to Software Development (ISD) David Weston and Igor Razgon

Computational Expression

Datatypes, Variables, and Operations

Algorithms and Programming I. Lecture#12 Spring 2015

Computer Programming: Skills & Concepts (CP) Variables and ints

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

Programming for Engineers in Python. Autumn

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CS 1428 Programming Assignment 2 Due Wednesday September 19 th :15 am Section 3 3:45 pm Section 4

PYTHON. Values and Variables

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Programming for Engineers in Python. Recitation 1

Introduction to Programming

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

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

Python Input, output and variables

IT 374 C# and Applications/ IT695 C# Data Structures

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

VBScript: Math Functions

Object-Oriented Programming

Fall 2017 CISC124 9/16/2017

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

ECE Lesson Plan - Class 1 Fall, 2001

Introduction to Java Applications

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Introduction to Microsoft Excel

Introduction Basic elements of Java

CPS122 Lecture: Defining a Class

CS 152 Computer Programming Fundamentals Coding Standards

Chapter 2 Working with Data Types and Operators

Introduction to the course and basic programming concepts

This presentation, titled Uploading Files with the Ext File Content Type is another installment in the series of training modules instructing faculty

CS 251 Intermediate Programming Coding Standards

COMP s1 Lecture 1

Using IDLE for

The price of gold. source: nowiknow.com. moodle.yorku.ca CSE 1020

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Experiment 18 Full Adder and Parallel Binary Adder

Tutorial No. 2 - Solution (Overview of C)

Lecture 02, Fall 2018 Friday September 7

An Introduction to Processing

1 Getting started with Processing

MODULE III: NAVIGATING AND FORMULAS

Tokens, Expressions and Control Structures

Copied from: on 3/20/2017

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

CSCI 2101 Java Style Guide

- HALF YEARLY EXAM ANSWER KEY DEC-2016 COMPUTER SCIENCE ENGLISH MEDIUM

ASSIGNMENT 2. COMP-202A, Fall 2011, All Sections. Due: Monday, October 17th, 2011 (23:30)

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

CE221 Programming in C++ Part 1 Introduction

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Queries with Multiple Criteria (OR)

C Language, Token, Keywords, Constant, variable

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

Previously. Iteration. Date and time structures. Modularisation.

Chapter 1 Introduction to Java

Identifiers and Variables

Lecture 5: Variables and Functions

Introduction to MATLAB

Java Programming Unit 3: Variables and Arithmetic Operations

Data types Expressions Variables Assignment. COMP1400 Week 2

PLAY VIDEO. 2. Select Cell Library File menu > New item to display the Create Cell Library dialog.

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

BLM2031 Structured Programming. Zeyneb KURT

CS18000: Problem Solving And Object-Oriented Programming

Java Program Coding Standards Programming for Information Technology

Data Types & Variables

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

DXi6900-S Software Installation and Upgrade Guide

INTRODUCTION 1 AND REVIEW

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Lecture 3. Functions & Modules

Short, Unique and Mysterious

Transcription:

Introduction to Programming Python Lab 2: Variables PythonLab2 lecture slides.ppt 19 January 2018 Ping Brennan (p.brennan@bbk.ac.uk) 1

Getting Started Create a new folder in your disk space with the name PythonLab2 Launch the Python Integrated Development Environment (IDLE) - begin with the Start icon in the lower left corner of the screen. If you are in a DCSIS laboratory, search using the keyword Python and click on IDLE (Python 3.6 64-bit) A window with the title Python 3.6.2 Shell should appear. This window is the Shell. 2

Getting Started (2) If you are in the ITS laboratory MAL 109, select the options in the order shown: Start -> All Programs -> Departmental Software -> Computer Science -> Python 3.4 -> IDLE (Python 3.4 GUI 64 bit) A window with the title Python 3.4.4rc1 Shell should appear. This window is the Shell. If the window does not appear, click on Start and then in the box Search programs and files write IDLE. A list will appear. Click on IDLE(Python 3.4 GUI-64 bit). A window with the title Python 3.4.4rc1 should appear. This window is the Shell. In the Shell click on File. A drop down menu will appear. Click on New File. A window with the title Untitled should appear. This window is the Editor. 3

Getting Started (3) In the Editor, click on File, and then in the drop down menu click on Save As. A window showing a list of folders should appear. To search any folder on the list, double click on the folder. Find the folder PythonLab2 and double click on it. In the box File name at the bottom of the window 1. Type Variables.py 2. Then click on the button Save in the lower right corner of the window. The title of the Editor should change to show the location of the file Variables.py. 4

Program to compute volumes Copy the following code, taken from Python for Everyone (PFE) Section 2.1.5, into the Editor. ## # This program computes the volume (in litres) of a six-pack of soda # cans and the total volume of a six-pack and a two-litre bottle. # # Litres in a 12-ounce can and a two-litre bottle. CAN_VOLUME = 0.355 BOTTLE_VOLUME = 2.0 # Number of cans per pack. cansperpack = 6 # Calculate total volume in the cans. totalvolume = cansperpack * CAN_VOLUME print("a six-pack of 12-ounce cans contains", totalvolume, "litres.") # Calculate total volume in the cans and a 2-litre bottle. totalvolume = totalvolume + BOTTLE_VOLUME print("a six-pack and a two-litre bottle contain", totalvolume, "litres.") 5

Program to compute volumes (2) Declaring variables in the Python program: Statements such as cansperpack = 6 assign values to variables. This is the first occurrence of the variable name cansperpack. Thus the variable is created and assigned a value, in this case 6. The type of the value is int. The statement CAN_VOLUME = 0.355 creates the variable CAN_VOLUME and assigns it a floatingpoint value of 0.355. The type of the value is float for any floating-point number that contains a fractional part. 6

Program to compute volumes (3) In the Editor click on Run, then on the drop down menu click on Run Module. A window will appear with the message Source Must Be Saved. Click on OK. The program in the Editor is saved to the file Variables.py and then run to produce the text from the two print statements in the Shell window. Move the statement cansperpack = 6 to the end of the program and check the compiler error message. Then move the statement back to the correct place. 7

Add code to the program to calculate purchase price By using two new variables, extend the previous program on slide 5 to calculate the total purchase price for the number of bottles that are purchased. Problem solving 1. Declare two new variables, unitprice and quantity : unitprice - contains the price of a single 2-litre bottle in dollars quantity - contains the number of bottles that are purchased Use reasonable initial values for the two variables, for example, quantity = 2 2. Calculate and print the total purchase price for the bottles. Use the formula below as the argument for the function print() : unitprice * quantity Convert the above solution into Python statements. Add comments in your code and update the comment at the top of the program. Then run your extended program. 8

Program to print the value of mystery Review question R2.1 in PFE which contains the following statements: mystery = 1 # line 1 mystery = 1-2 * mystery # line 2 mystery = mystery + 1 # line 3 Create a new Editor for a new file called Mystery.py Copy the above code into the Editor. Problem solving 1. Print the initial value of the variable mystery after line 1 in the above code. 2. Print the final value of the variable mystery after line 3 in the above code. Convert the above solution into two Python statements that call the function print(). Add a comment at the top of the source file to explain the origin and purpose of the code. Then run your program. 9

Program to calculate square root of 2 Create a new Editor for a new file called Sqrt2.py. Type the following code into the Editor. # Initialize x x=1.0 print("x*x:", x*x) # First iteration delta = (1/x)-x/2 x = x+delta print("x*x:", x*x) # Second iteration delta = (1/x)-x/2 x = x+delta print("x*x:", x*x) Add a comment at the top of the source file to describe the origin and purpose of the program. 10

Program to calculate square root of 2 (2) Problem solving (extend the program on slide 10) 1. Add a third iteration to determine the new delta and x values, and then print the square of x (i.e. x*x) 2. Add a fourth iteration to determine the new delta and x values, and then print the square of x (i.e. x*x) Convert the above solution into Python statements and add these to the end of the code on slide 10. Run your program. Examine the numbers that are printed out. 11

Supplementary Questions for Private Study The laboratory worksheet contains supplementary questions 6.1, 6.2 and 6.3 for private study. You are encouraged to complete the supplementary questions at home, or in the laboratory if you have time after completing questions 2 to 5. 12

Appendix A Variable Names (Python For Everyone, Section 2.1.3) When you define a variable, you need to give it a name that describes its purpose. You must follow a few simple rules when naming something in Python: Names must start with a letter or the underscore (_) character, and the remaining characters must be letters, numbers, or underscores. Other symbols such as? or % cannot be used, and spaces are not permitted inside names. Names are case sensitive in Python. Python reserved words cannot be used as names. Recommended practice: Use descriptive names for variables. Begin names of variables with a lowercase letter. By convention, name of a variable that is all uppercase indicates a constant, that is the value of the variable does not change. 13