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

Similar documents
Variables, expressions and statements

Act like a code monkey Coding Basics

Algorithms and Programming I. Lecture#12 Spring 2015

Getting Started with Python

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

Fundamentals of Programming (Python) Getting Started with Programming

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

Python for Informatics

PROGRAMMING FUNDAMENTALS

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

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

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

Python Programming Exercises 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Here n is a variable name. The value of that variable is 176.

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

cs1114 REVIEW of details test closed laptop period

Full file at

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

Python for Non-programmers

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

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

ENGR 101 Engineering Design Workshop

Variables, Expressions, and Statements

Topic 2: Introduction to Programming

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

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

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

Expressions and Casting

egrapher Language Reference Manual

The current topic: Python. Announcements. Python. Python

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

Ch.2: Loops and lists

Computing with Numbers Zelle - Chapter 3

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

DaMPL. Language Reference Manual. Henrique Grando

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Full file at

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

Programming in Python 3

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Chapter 2 Working with Data Types and Operators

Lecture 1. Types, Expressions, & Variables

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

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

Lecture 3. Input, Output and Data Types

An Introduction to Python (TEJ3M & TEJ4M)

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

Conditionals and Recursion. Python Part 4

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

Python Unit

CSE 115. Introduction to Computer Science I

Unit E Step-by-Step: Programming with Python

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

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

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

Conditional Expressions and Decision Statements

Language Reference Manual

CS313D: ADVANCED PROGRAMMING LANGUAGE

Visual C# Instructor s Manual Table of Contents

COMP 202 Java in one week

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

Test #2 October 8, 2015

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

Reserved Words and Identifiers

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

CS 115 Lecture 8. Selection: the if statement. Neil Moore

COMP 202. Java in one week

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

CS 1301 Exam 1 Answers Fall 2009

Variable and Data Type I

Beyond Blocks: Python Session #1

Python Day 3 11/28/16

Introduction to Python

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Ruby: Introduction, Basics

Getting Started Values, Expressions, and Statements CS GMU

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Introduction to TURING

A simple interpreted language

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Computer Programming : C++

Lesson 3: Basic Programming Concepts

Introduction to Programming Using Java (98-388)

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

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

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

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

Introduction to Python

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CMSC 201 Computer Science I for Majors

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

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

Transcription:

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

Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. 2

The Python programming language High-level language. The engine that translates and runs Python is called the Python Interpreter. Immediate mode Script mode 3

What is a program? A program is a sequence of instructions that specifies how to perform a computation. input Get data from the keyboard, a file, or some other device. output Display data on the screen or send data to a file or other device. math Perform basic mathematical operations like addition and multiplication. conditional execution Check for certain conditions and execute the appropriate sequence ofstatements. repetition Perform some action repeatedly, usually with some variation. 4

What is debugging? Programming errors are called bugs and the process of tracking them down and correcting them is called debugging. Syntax errors Syntax refers to the structure of a program and the rules about that structure. Python will display an error message and quit, and you will not be able to run your program. Semantic errors Runtime errors If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing. Error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened. 5

The first program Install Python Install pyscripter https://www.youtube.com/watch?v=1hz38rzykue 6

VARIABLES, EXPRESSIONS AND STATEMENTS 7

Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. Values are classified into different classes, or data types: 4 is an integer, and "Hello, World!" is a string, Python has a function called type which can tell you. 8

Values and data types examples >>> type("hello, World!") <class str > >>> type(17) <class int > >>> type("17") <class str > >>> type("3.2") <class str > >>> type(3.2) <class float > >>> type( This is a string. ) <class str > >>> type("and so is this.") <class str > >>> type("""and this.""") <class str > >>> type( and even this... ) <class str > 9

Self -test How many lines of screen output is displayed by the following, print('apple\nbanana\ncherry\npeach') (a) 1 (b) 2 (c) 3 (d) 4 Which of the following are valid string literals in Python. (a) "Hello" (b) 'hello' (c) "Hello' (d) 'Hello there' (e) '' 10

Variables A variable is a name that is associated with a value. The assignment operator = is used to assign values to variables. The id function produces a unique number identifying a specifi c value (object) in memory 11

Variables example 12

Keywords and Statements A statement is an instruction that the Python interpreter can execute. Ex: if, else, import 13

Evaluating expressions An expression is a combination of values, variables, operators, and calls to functions. >>> 1 + 1 2 >>> len("hello") 5 14

Operators and operands Operators are special tokens that represent computations like addition, multiplication and division. The values the operator uses are called operands. The tokens +, -, and *, and the use of parenthesis for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the token for multiplication, and ** is the token for exponentiation. 15

Type converter functions >>> int(3.14) 3 >>> float(17) 17.0 >>> float("123.45") 123.45 >>> str(17) 17 >>> str(123.45) 123.45 16

Operations on strings Interestingly, the + operator does work with strings, but for strings, the + operator represents concatenation, not addition. The * operator also works on strings; it performs repetition. 1 fruit = "banana" 2 baked_good = " nut bread" 3 print(fruit + baked_good) Fun *3 is FunFunFun. The output of this program is banana nut bread. 17

format The built-in format function can be used to produce a numeric string of a given fl oating-point value rounded to a specifi c number of decimal places.... format(13402.25, ',.2f') 13,402.24 18

Input There is a built-in function in Python for getting input from the user: n = input("please enter your name: ") 19

Let s try a simple program P1. Write a Python program that prompts the user for two floating-point values and displays the result of the first number divided by the second, with exactly six decimal digits of precision displayed. print("{:.6f}".format(c)) P2. Calculate the area of a circle 20

Let s try a simple program P5. Write a Python program that allows the user to enter two integer values, and displays the results when each of the following arithmetic operators are applied. For example, if the user enters the values 7 and 5, the output would be, All floating-point results should be displayed with two decimal places of accuracy. In addition, all values should be displayed with commas where appropriate. 21

CONDITIONALS 22

Boolean values and expressions A Boolean value is either true or false. In Python, the two Boolean values are True and False. >>> type(true) <class bool > >>> type(true) Traceback (most recent call last): File "<interactive input>", line 1, in <module> NameError: name true is not defined 23

Boolean values and expressions A Boolean expression is an expression that evaluates to produce a result which is a Boolean value. 24

Conditional execution The simplest form is the if statement: 25

Conditional execution The simplest form is the if statement: 26

Chained conditionals 27

Nested conditionals 28