Getting Started with Python

Similar documents
Fundamentals of Programming (Python) Getting Started with Programming

Algorithms and Programming I. Lecture#12 Spring 2015

ENGR 101 Engineering Design Workshop

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

Chapter 2 Writing Simple Programs

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

Introduction to Problem Solving and Programming in Python.

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

Variable and Data Type I

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

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

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

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

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

Variables, Expressions, and Statements

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

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

Jython. secondary. memory

Act like a code monkey Coding Basics

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Python for Informatics

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

PROGRAMMING FUNDAMENTALS

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

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

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

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

Conditional Expressions and Decision Statements

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

Programming in Python 3

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

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

Python for Non-programmers

Lecture 4: Basic I/O

Scripting Languages. Python basics

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

COMP 202 Java in one week

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

Table of Contents EVALUATION COPY

Introduction to Python

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

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

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

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

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

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

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Short, Unique and Mysterious

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Programming for Engineers in Python. Recitation 1

Python Programming Exercises 1

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

The current topic: Python. Announcements. Python. Python

Debugging & Errors Why you were up till 2AM

CSCE 110 Programming I

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

3. Java - Language Constructs I

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

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

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Detailed guide for learning to program in Python 3

Basic Syntax - First Program 1

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

And Parallelism. Parallelism in Prolog. OR Parallelism

Control Structures 1 / 17

Python Programming: An Introduction to Computer Science

Beyond Blocks: Python Session #1

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

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

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

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

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

Outline. software testing: search bugs black-box and white-box testing static and dynamic testing

Introduction to Programming Using Java (98-388)

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

Topic 2: Introduction to Programming

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Introduction to Computer Programming for Non-Majors

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

Lecture 3. Input, Output and Data Types

Chapter 9: Dealing with Errors

Programming for Engineers in Python. Autumn

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

PHPoC vs PHP > Overview. Overview

Lecture 4. Defining Functions

CMSC 201 Computer Science I for Majors

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Full file at

Not-So-Mini-Lecture 6. Modules & Scripts

Getting Started Values, Expressions, and Statements CS GMU

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

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

Coral Programming Language Reference Manual

Transcription:

Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

Outline 1. Software Development Paradigm 2. Defining the Problem 3. Creating a Design 4. Coding with Python 5. Testing the Program 2

Software Development 1. Define the Problem Objectives? Inputs? Outputs? Process? 2. Create a Design With algorithm & flowcharts 3. Code the Program Using a programming language (Python) 4. Test the Program Write appropriate tests & see if you get the right answers Use the Debugging system 3

Defining the Problem Celsius-Fahrenheit Conversion Objective The temperature is given in Celsius, user wants it expressed in degrees Fahrenheit. Input Temperature in Celsius Output Temperature in Fahrenheit Process Fahrenheit = 9 Celsius + 32 5 4

Creating a Design Algorithm a well-defined recipe for solving a problem Has a finite number of steps Completes in a finite amount of &me Often referred to as pseudocode Celsius-Fahrenheit Algorithm 1. Begin 2. C input from user 3. F C x 9/5 + 32 4. Output F 5. End 5

Creating a Design Flowchart A graphical model to represent an algorithm Steps are shown with boxes of different shapes The flow is specified by arrows Begin Input C F C x 9/5 + 32 Terminal Decision Process Input/ output Output F End 6

Coding with Python When you start Python, you will see something like: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements. 7

Coding with Python The first Program: Printing a line of text The print function sends a stream of text to the standard output >>> print("hello world") Hello world 8

Coding with Python The second program: Ask user s name The input function receives a stream of text from standard input >>> user = input( What s your name? ") What s your name? Sina >>> print("hello", user) Hello Sina 9

Variables in Python >>> user = input( What s your name? ") user is an example of a variable A variable is used to assign a name to a value so that we can refer to it later. Variables have names, called identifiers 10

Variables in Python A variable name (identifier) can be any one word that: Consists of letters, numbers, or _ Does not start with a number Is not a Python reserved word (keyword) 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 return def for lambda try Python is case-sensitive: User is not the same as user 11

Data Types Python provides some basic or primitive data types Numeric int 25, -32, 0, 1024 float 1.5, 0.0025, -80.635 complex Boolean True False String 1+3j, -2.5+j, 12j this is a string, 25, False 12

Arithmetic Operations Symbol Meaning Precedence + Addition Low - Subtraction Low * Multiplication Medium / Division Medium // Floor Division Medium % Reminder (mod) Medium ** Exponentiation (Power) High 13

A Complete Program Celsius-Fahrenheit Python Program >>> c = input("enter temperature in Celsius: ") >>> f = c * 9/5 + 32 >>> print("temperature in Fahrenheit:", f) 14

A Complete Program Celsius-Fahrenheit Python Program >>> c = input("enter temperature in Celsius: ") >>> f = c * 9/5 + 32 >>> print("temperature in Fahrenheit:", f) Any Problems? The input function returns a string Needs to be converted into float >>> c = float(c) 15

Type Conversion Data type converter functions To Integer >>> x = int("25") >>> x = int(34.287) >>> x = int(true) To Float >>> x = float("34.287") >>> x = float(12) >>> x = float(false) To String >>> x = str(34.287) >>> x = str(12) >>> x = str(true) To Boolean >>> x = bool("text") >>> x = bool(0) >>> x = bool(34.287) 16

Put All Together Celsius-Fahrenheit Python Program >>> c = input("enter temperature in Celsius: ") >>> f = float(c) * 9/5 + 32 >>> print("temperature in Fahrenheit:", f) Want to save your program? Let s switch to script mode! 17

Python Running Modes Interactive Mode Write code directly in interpreter command line shell Gives immediate feedback for each statement Best for playing around with your code Script Mode Write code into a file and save it with.py extension Give the file as input to the interpreter Best for running the program multiple times 18

Test the Program Programming often leads to Error! Programming errors are called bugs Tracking the bugs and correcting them is called debugging Different error types Syntax error Runtime error Semantic error 19

Programming Errors Syntax Error Caused by the violation of rules and structure of Python language Found when the code is being interpreted to machine language. Pretty easy to catch Example: choosing a keyword as a name for a variable >>> lambda = 1.5 File "<stdin>", line 1 lambda = 1.5 ^ SyntaxError: invalid syntax 20

Programming Errors Runtime Error (Exception) Caused due to many reasons, such as hardware failure, access violation, memory errors, Found while the code is running Moderate to catch (need coding) Example: using operators with incorrect operand types >>> x = "text" >>> x / 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'str' and 'int' 21

Programming Errors Semantic Error Caused due to incorrect coding or design No error messages are generated at all! Tricky to catch (need code inspection) Example: incorrect use of operator >>> c = input("enter temperature in Celsius: ") >>> f = float(c) * 9//5 + 32 >>> print("temperature in Fahrenheit:", f) 22

Experimental Debugging An important skill Frustrating but challenging Sounds like detective work Follow the clues to find the source of error Get help from a debugger IDEs usually come with a debugger Helps to find bugs using debugging tools such as breakpoints, profilers, etc. 23