Lecture 1. basic Python programs, defining functions

Similar documents
Introduction to Python

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

Python! Created in 1991 by Guido van Rossum (now at Google)

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Python Input, output and variables

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

The current topic: Python. Announcements. Python. Python

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Table of Contents EVALUATION COPY

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science

The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.

Welcome to Python 3. Some history

Introduction to computers and Python. Matthieu Choplin

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 190C: Introduction to Computational Thinking

Python Basics. level 1 Chris Roffey

Introduction to Computer Programming for Non-Majors

What you get When you install Python for your computer, you get a number of features:

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

Introduction to Python for Scientific Computing

Accelerating Information Technology Innovation

ASCII Art. Introduction: Python

Algorithms and Programming I. Lecture#12 Spring 2015

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

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

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

Programming Languages: Part 1. Robert M. Dondero, Ph.D. Princeton University

CS 177 Recitation. Week 1 Intro to Java

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

QUIZ: What value is stored in a after this

DSC 201: Data Analysis & Visualization

Programming Languages Part 1. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Computer Science Seminar. Whats the next big thing? Ruby? Python? Neither?

Senthil Kumaran S

Accelerating Information Technology Innovation

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Week 2. expressions, variables, for loops

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

4. Structure of a C++ program

Introduction to Python. Didzis Gosko

Introduction to Python

Chapter 2 Getting Started with Python

Unit 7: Algorithms and Python CS 101, Fall 2018

Python Unit

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Python notes from codecademy. Intro: Python can be used to create web apps, games, even a search engine.

Programming for AmI MOTIVATIONS AND GOALS

Rapid Application Development with

Intro to Python & Programming. C-START Python PD Workshop

CSI Lab 02. Tuesday, January 21st

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

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

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

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

Full file at C How to Program, 6/e Multiple Choice Test Bank

Lecture 4. Defining Functions

Invent Your Own Computer Games with Python

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Short, Unique and Mysterious

Winter 2019 CISC101 1/17/2019

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Scientific Computing using Python

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

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

LECTURE 1. Getting Started with Python

Pythonic Coding Style. C-START Python PD Workshop

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

Week - 01 Lecture - 04 Downloading and installing Python

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Introduction to Programming

Python - a Dynamic Programming Language. Guido van Rossum May 31, 2007

Welcome to Python! Ilhoe Jung. Graphics & Media Lab

Programming for Engineers in Python. Recitation 1

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

And Parallelism. Parallelism in Prolog. OR Parallelism

Introduction to Python - Part I CNV Lab

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

Programming Environments

nostarch.com/pfk For bulk orders, please contact us at

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Introduction to Python, Cplex and Gurobi

Introduction to programming with Python

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

COMP Assignment 1

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

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

Some material adapted from Upenn cmpe391 slides and other sources

Full file at

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

Lesson 4: Who Goes There?

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

Getting Started with Python and the PyCharm IDE

PROGRAMMING FUNDAMENTALS

MEIN 50010: Python Introduction

Transcription:

Lecture 1 basic Python programs, defining functions Lecture notes modified from CS Washington CS 142 Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

Python! Created in 1991 by Guido van Rossum (now at Google) Named for Monty Python Useful as a scripting language script: A small program meant for one-time use Targeted towards small to medium sized projects Used by: Google, Yahoo!, Youtube Many Linux distributions Games and apps (e.g. Eve Online) 2

Interpreted Languages interpreted Not compiled like Java Code is written and then directly executed by an interpreter Type commands into interpreter and see immediate results Java/C: Code Compiler Runtime Environment Computer Python: Code Interpreter Computer 3

The Python Interpreter Allows you to type commands one-at-a-time and see results A great way to explore Python's syntax Repeat previous command: Alt+P Next command: Alt+N 4

Running Python Program Python does not have a main method like Java or C The program's main code is just written directly in the file Python statements do not end with semicolons Python two running modes: Interactive Python Shell mode IDLE Python source file create, edit, and running 5

Python Interactive Shell When you start up IDLE, a window with an interactive Python shell will pop up: You can type Python code directly into this shell, at the '>>>' prompt. Whenever you enter a complete code fragment, it will be executed. 6

Python Interactive Shell Python Interactive Shell Practice: The print command outputs text directly to the Python shell >>> print( hello there ) hello there >>>print( ) >>>print(4+6) 10 >>>print(8**3) 512 7

Number vs String Number: Numeric value can be added, subtracted, multiplied, divided, etc String: a string value is a value that can contain any combinations of characters, it'll normally get used for an address or similar, you can put what you want there really. In Python, single quote, hello or double quotes hello In Python Shell prompt, try >>> print(123+53) 176 >>>print( 123+53 ) 123+53 More on these will be covered later. 8

Python IDLE File In IDLE, go to FILE New Create a new python file (Open -> open an existing Python file) You can edit/modify/create a Python file and run it (IDLE- >RUN-> RUN MODULE) Demonstrate, and create a new hello.py file with these python commands. 9

A Brief Review 10

The print Statement print("text") print() (a blank line) Certain special characters must be prefixed with a backslash Strings can also start/end with ' swallows.py 1 2 3 4 print("hello, world!") print() print("suppose two swallows \"carry\" it together.") print('african or "European" swallows?') 11

Comments Comments are ignored Syntax: # comment text (anything after the # on the same line is ignored) swallows2.py 1 2 3 4 5 6 # Suzy Student, Raspberry Pi Camp, Summer 2016 # This program prints important messages. print("hello, world! ) print() # blank line print( Suppose two swallows \"carry\" it together. ) print('african or "European" swallows? ) 12

Functions Function: A block of Python commands executed together. Syntax: def name(): statement statement... statement hello2.py 1 2 3 4 5 6 7 # Prints a helpful message. def hello(): print("hello, world! ) # main (calls hello twice) hello() hello() Must be declared above the 'main' code Statements inside the function must be indented 13

Whitespace Significance Python uses indentation to indicate blocks Makes the code simpler and more readable In Java/C, indenting is optional. In Python, you must indent. hello3.py 1 2 3 4 5 6 7 8 # Prints a helpful message. def hello(): print("hello, world! ) print("how are you? ) # main (calls hello twice) hello() hello() 14

Exercise Rewrite the figures.py lecture program in Python, so it outputs: / \ / \ \ / \ / \ / \ / +--------+ / \ / \ STOP \ / \ / / \ / \ +--------+ 15

Exercise Solution def egg(): top() bottom() print def cup(): bottom() line() print def stop(): top() print(" STOP ) bottom() print def hat(): top() line() print def top(): print(" ) print(" / \\ ) print("/ \\ ) def bottom(): print("\\ / ) print(" \\ / ) def line(): print("+--------+ ) # main egg() cup() stop() hat() 16