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

Getting Started with Python

Variable and Data Type I

Algorithms and Data Structures

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

Table of Contents EVALUATION COPY

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

Python Programming Exercises 1

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

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

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

Basic Syntax - First Program 1

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Chapter 2 Writing Simple Programs

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

Variables, Expressions, and Statements

Short, Unique and Mysterious

CSCE 110 Programming I

CMSC 201 Computer Science I for Majors

Jython. secondary. memory

Topic 2: Introduction to Programming

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

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

The float type and more on variables FEB 6 TH 2012

Getting started 7. Saving data 23

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

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

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

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

About Variables in Python F E B 1 1 T H

Introduction to Programming

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

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

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

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

And Parallelism. Parallelism in Prolog. OR Parallelism

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

Python for ArcGIS. Lab 1.

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

Python Unit

Programming with Python

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Introduction to Python, Cplex and Gurobi

Introduction to Problem Solving and Programming in Python.

Introduction to Java Applications

Python Input, output and variables

Invent Your Own Computer Games with Python

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

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

Introduction to programming with Python

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

What Version Number to Install

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

FIT9133 Module 1: Introduction to Algorithms and Python Programming Basics

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

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

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

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

Variables and Constants

CSCE 120: Learning To Code

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

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

Expressions and Variables

Introduction to computers and Python. Matthieu Choplin

COMP 202 Java in one week

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

Our Strategy for Learning Fortran 90

Dr Richard Greenaway

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

Chapter 2 Getting Started with Python

Chapter 10 Language Translation

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

GBL Language Reference Manual

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

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

CSI Lab 02. Tuesday, January 21st

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

Computational Physics

Chapter 1 Operations With Numbers

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

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

D R S H YA M N C H AW D A

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

[CHAPTER] 1 INTRODUCTION 1

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

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

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

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

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

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

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) scl enable python27 bash < CR > (carriage return) (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 1

(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 have the same order. v. example 3*(2-2) is 0 vi. example (2+1)*(4/2) is 6 vii. example 3**2+1 is 10 not 27 viii. example: 2.0/2.0/2.0 gives the same result as 2.0/(2.0*2.0), that is 0.5 ix. the output of 2/5 is zero because python 2 performs floor division and gets rid of digits after the decimal place if the two numbers are both integers. Python 3 will output 0.4 and floor division is performed by a special operator. x. < 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. 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 3

state of the memory location that we are calling x is the float value 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 changes dynamically. 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. Consider the following IDLE conversation containing three illegal names illustrating that 4

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