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

Similar documents
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

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Data Structures

Python Programming Exercises 1

Variable and Data Type I

Getting Started with Python

Variable and Data Type I

ENGR 101 Engineering Design Workshop

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

Fundamentals of Programming (Python) Getting Started with Programming

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

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

Basic Syntax - First Program 1

Chapter 2 Writing Simple Programs

Table of Contents EVALUATION COPY

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

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

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

Jython. secondary. memory

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Short, Unique and Mysterious

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

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

Variables, Expressions, and Statements

CMSC 201 Computer Science I for Majors

Topic 2: Introduction to Programming

CSCE 110 Programming I

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

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

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

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

The float type and more on variables FEB 6 TH 2012

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

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

Python for ArcGIS. Lab 1.

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

Getting started 7. Saving data 23

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

Programming with Python

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

Intro. Scheme Basics. scm> 5 5. scm>

Introduction to Python, Cplex and Gurobi

About Variables in Python F E B 1 1 T H

Introduction to Programming

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

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

Python Unit

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

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

Variables and Constants

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

CSCE 120: Learning To Code

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

Introduction to Java Applications

And Parallelism. Parallelism in Prolog. OR Parallelism

FIT9133 Module 1: Introduction to Algorithms and Python Programming Basics

Starting. Read: Chapter 1, Appendix B from textbook.

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Computer Components. Software{ User Programs. Operating System. Hardware

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Dr Richard Greenaway

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Expressions and Variables

BASIC ELEMENTS OF A COMPUTER PROGRAM

Python Input, output and variables

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

Introduction to computers and Python. Matthieu Choplin

Introduction to Problem Solving and Programming in Python.

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Invent Your Own Computer Games with Python

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

Chapter 2 Getting Started with Python

Introduction to programming with Python

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

How to Think Like a Computer Scientist: Learning with Python 3 Documentation

What Version Number to Install

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

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

MEIN 50010: Python Introduction

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

S E C T I O N O V E R V I E W

Lab # 2. For today s lab:

VBScript: Math Functions

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

CSI Lab 02. Tuesday, January 21st

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Python for Non-programmers

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

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

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

egrapher Language Reference Manual

Chapter 1 Operations With Numbers

GBL Language Reference Manual

Exercise: Inventing Language

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Transcription:

I. INTERACTIVE MODE VERSUS SCRIPT MODE There are two ways to use the python interpreter: interactive mode and script mode. 1. Interactive Mode (a) open a terminal shell (terminal emulator in Applications Menu) (b) You may or may not have to type the following command: scl enable python27 bash < CR > (carriage return) as shown in the figure below. (c) idle < CR > (d) This will open a new IDLE window (Interactive Development and Learning Environment). The chevron >>> is the prompt telling you that IDLE is ready to execute an instruction. In the IDLE conversation shown above, you can see how the interpreter is 1

being used in interactive mode as a simple calculator: You type an instruction to the interpreter and after pressing < CR >, the instruction is carried out. (e) The usual order of operations (PEDMAS) is followed by the interpreter. (Parenthesis-Exponentiation-Division-Multiplication-Addition-Subraction) i. expressions in parenthesis are evaluated first, followed by the exponentiation operation. ii. division and multiplication are evaluated next and have the same order. iii. Operations of the same order are evaluated left to right. iv. addition and subtraction are evaluated last and have the same order. v. Note the difference in output between an instruction to divide two integers (2/5) and two floats (2.0/5.0). This is because python 2 performs floor division for integers and gets rid of digits after the decimal place. Integers and floats are two different data types which is a concept that will be discussed below. Python 3 outputs 2/5=0.4 and floor division is performed by a special operator. vi. < CT RL > D exits IDLE 2. Script Mode (a) You can also save your commands in a text file, which by convention have the suffix.py, for example, program.py. (b) Create your list of commands inside any text editor, for example gedit. Save this program as hello.py. (c) run the program as follows (you have to be sure that you are in the directory where the script file hello.py is located) 2

II. VALUES AND TYPES A value, such as 1,2, 3.14 or Hello World is stored in a location in computer memory. The values belong to different types: (i) integer (e.g. 1,2,3), (ii) float (3.14) or (iii) string - so called because it contains a string of characters (e.g. Hello, World! ). Different types are stored in different ways in computer memory. The interpreter command type() can be used to determine type. type() is an example of a function. Whatever you type between the brackets is called the argument of the function. Open IDLE and enter the commands shown above. Can you explain each python response? Can you explain the syntax error? III. VARIABLES A variable is a name given to a piece of computer memory. One reads the assignment statement x=5.0 as meaning place the float value 5.0 in memory location called x. One might also say the state of the memory location that we are calling x is the float value 3

5.0. Recreate the following IDLE conversation where values are assigned to the variables n, e and message : One can see that the variable type is determined by the assignment statement. recreate IDLE conversation below If you one will see that the type of a particular variable is not fixed, but can be changed by an assignment statement. IV. VARIABLE NAMES AND KEYWORDS A programmer should strive to use meaningful names that enhance the readability of a program. Variable Names can be arbitrarily long. They can contain both letters and numbers and are case-sensitive, but they must begin with a letter. That is ArKiv and arkiv are different variable names. To make the names meaningful, consider using underscores for very long names such as speed of a bullet to improve readability of program. If underscores are not used, you will get a syntax error. For example, fun fact = 100.0 is illegal but fun fact=100.0 is legal. 4

Consider the following IDLE conversation containing three illegal names illustrating that variable names must start with a letter, cannot contain illegal symbols (@,$ etc.) and cannot be one of Python 2 s 31 keywords. The interpreter uses the keywords to recognize the structure of the program and hence they cannot be used as variable names. in Python 3, exec is no longer a keyword but nonlocal is. The list of python 2 keywords is as follows: and del from not while as elif global or with assert else if pass yield break else if pass class exec in raise continue finally is return def for lambda try V. COMMENTS Obviously, complicated programs are run in script mode. As programs get larger and more complicated, they get more difficult to read. It is a good idea to add notes to explain what the program is doing, and non-obvious features of the code. This is true if more than one programmer is working on a project, or if a single programmer is writing a long program. It is very often difficult to recall exactly what your program is doing. Called comments, these notes start with the # symbol. Anything after the comment symbol in a line is ignored by the interpreter. 5

6

VI. EXERCISES I These exercises are copied from Think Python, by Allen Downey. 1. Start IDLE and type help() to start the online help utility to get information about the print statement. 2. Start IDLE and use it as a calculator: (a) to determine the average speed (in km/h) of Sir Roger Bannister who, in 1954, was the first person to run a mile in under four minutes (3 minutes, 58 seconds). (b) to determine the number of seconds in a year. (c) Determine the volume of a sphere 4 3 πr3 of radius 7 cm. Hint: Using π = 3.1416, the value 1077.5688 is wrong. 3. Write a program using gedit or another text editor that will print a well-structured English sentence with invalid tokens in it and run it in script mode. (See the introduction for definition of token) 4. Write a program using gedit or another text editor that will print an English sentence with valid tokens but with an invalid structure and and run it in script mode. (See the introduction for definitions of tokens, structure and syntax) 5. What is the problem in the following IDLE conversation? Is this a semantic error or a syntax error? 6. If you begin an assignment statement with a leading zero, you might get confusing errors. 7

Can you figure out what is going on? Hint: display the values of 01, 010,0100,01000. 8