Lecture 05: Basic Python Programming

Similar documents
Lecture 06: Compound Data Types in Python

CME 193: Introduction to Scientific Python Lecture 1: Introduction

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

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started with Python

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

Multiprotocol Label Switching The future of IP Backbone Technology

Control Structures 1 / 17

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

Lecture 07: Python Generators, Iterators, and Decorators

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

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

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

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

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

Variables, Expressions, and Statements

Command Dictionary CUSTOM

Understanding IO patterns of SSDs

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Programming to Python

信息检索与搜索引擎 Introduction to Information Retrieval GESC1007

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

Lecture 04: Bash Scripting

Introduction to Python

Downloaded from Chapter 2. Functions

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

计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法. 授课老师 : 王浩宇

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

NyearBluetoothPrint SDK. Development Document--Android

Programming in Python 3

Lecture 4. Defining Functions

1. Features. 2,Block diagram. 3. Outline dimension V power supply. 3. Assembled with 20 x 4 character displays

2.8 Megapixel industrial camera for extreme environments

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

5.1 Megapixel machine vision camera with GigE interface

<properties> <jdk.version>1.8</jdk.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties>

STA141C: Big Data & High Performance Statistical Computing

Beyond Blocks: Python Session #1

CSCE 110 Programming I

Chapter 2: Relational Model

2. Introduction to Digital Media Format

The current topic: Python. Announcements. Python. Python

Altera 器件高级特性与应用 内容安排 时钟管理 时钟管理 片内存储器 数字信号处理 高速差分接口 高速串行收发器. 时钟偏斜 (skew): 始终分配到系统中到达各个时钟末端 ( 器件内部触发器的时钟输入端 ) 的时钟相位不一致的现象 抖动 : 时钟边沿的输出位置和理想情况存在一定的误差

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

ENGR 101 Engineering Design Workshop

Get It Interpreter Scripts Arrays. Basic Python. K. Cooper 1. 1 Department of Mathematics. Washington State University. Basics

Murrelektronik Connectivity Interface Part I Product range MSDD, cable entry panels MSDD 系列, 电缆穿线板

Table of Contents EVALUATION COPY

Python for Finance. Introduction and Basics of Python. Andras Niedermayer

Technology: Anti-social Networking 科技 : 反社交网络

Lecture 1. Types, Expressions, & Variables

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

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

Built-in Types of Data

Compile times - assert macros

正则表达式 Frank from

Computer Networks. Wenzhong Li. Nanjing University

18.1. CS 102 Unit 18. Python. Mark Redekopp

Bi-monthly report. Tianyi Luo

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

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Lecture 0. Introduction to Python. Randall Romero Aguilar, PhD II Semestre 2018 Last updated: July 12, 2018

Chapter 1 (Part 2) Introduction to Operating System

Safe Memory-Leak Fixing for C Programs

cs1114 REVIEW of details test closed laptop period

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

信息检索与搜索引擎 Introduction to Information Retrieval GESC1007

Variables, expressions and statements

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

Lecture 3 for pipelining

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

Data Handing in Python

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

Python Programming, bridging course 2011

Chapter2 Instruction Sets

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

ECE 122 Engineering Problem Solving with Java

Introduction to Computer Science

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

New Media Data Analytics and Application. Lecture 7: Information Acquisition An Integration Ting Wang

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

三 依赖注入 (dependency injection) 的学习

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

1. DWR 1.1 DWR 基础 概念 使用使用 DWR 的步骤. 1 什么是 DWR? Direct Web Remote, 直接 Web 远程 是一个 Ajax 的框架

Advanced Algorithms and Computational Models (module A)

信息检索与搜索引擎 Introduction to Information Retrieval GESC1007

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

And Parallelism. Parallelism in Prolog. OR Parallelism

上汽通用汽车供应商门户网站项目 (SGMSP) User Guide 用户手册 上汽通用汽车有限公司 2014 上汽通用汽车有限公司未经授权, 不得以任何形式使用本文档所包括的任何部分

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Spark Standalone 模式应用程序开发 Spark 大数据博客 -

ICP Enablon User Manual Factory ICP Enablon 用户手册 工厂 Version th Jul 2012 版本 年 7 月 16 日. Content 内容

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

云计算入门 Introduction to Cloud Computing GESC1001

学习沉淀成长分享 EIGRP. 红茶三杯 ( 朱 SIR) 微博 : Latest update:

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

MEIN 50010: Python Introduction

: Operating System 计算机原理与设计

Transcription:

BI296: Linux and Shell Programming Lecture 05: Basic Python Programming Maoying,Wu ricket.woo@gmail.com Dept. of Bioinformatics & Biostatistics Shanghai Jiao Tong University Spring, 2017 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 1 / 44

Lecture Outline Python: Introduction ( 简介 ) Installing Python (Python 安装 ) Run Python( 运行 Python 的方式 ) Data Types ( 数据类型 ) Basic types( 基本类型 ):number( 数值, 包含 int( 整型 ), long( 长整型 ), float( 浮点数 ), complex( 复数 )) str( 字符串 ) bool( 布尔 ) Container( 容器类型 ): list ( 列表 ), tuple ( 元组 ), dict ( 字典 ), set ( 集合 ) Operators( 操作符 ) Arithmetic operators ( 数学操作符 ):+, -, *, /, %, ** Logical operators ( 逻辑操作符 ):and, or, not Comparison operators ( 比较操作符 ): <, <=, >, >=, ==,!=, is Assignment operators ( 赋值操作符 ): = Membership operators ( 成员操作符 ):in Conditional Expression and Flow Control( 条件表达式和流程控制 ) Functions ( 函数的定义 ) General( 一般函数 )and lambda function ( 单行匿名函数 ) Moules ( 模块 ) Maoying Wu (CBB) BI296-Lec05 Spring, 2017 2 / 44

Next we will talk about... 1 Introduction 2 Data types 3 flow control 4 functions Maoying Wu (CBB) BI296-Lec05 Spring, 2017 3 / 44

Come and Join Python Maoying Wu (CBB) BI296-Lec05 Spring, 2017 4 / 44

What is Python? Interpreted language ( 解释性编程语言 ). Relatively easy to learn. Fast to write code. Intuitive. Very versatile (vs. Matlab/R). Less control, worse performance (vs. C). Less safety handles, responsibility for user. Extremely popular in the field of data science. Matrix computing Statistics, machine learning Visualization Maoying Wu (CBB) BI296-Lec05 Spring, 2017 5 / 44

Install Python ( 安装 ) I recommend installing the Pre-package Python - Anaconda https://repo.continuum.io/archive/anaconda2-4.3. 1-Linux-x86_64.sh Use Python 2.7 Run bash Anaconda2-4.3.1-Linux-x86_64.sh. Add to the environment variable PATH. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 6 / 44

Run Python interactively ( 交互式运行 ) Invoke the command python Python 2.7.12 Anaconda custom (64-bit) (default, Jul 2 2016, 17:42:40) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org >>> Invoke the command ipython Python 2.7.12 Anaconda custom (64-bit) (default, Jul 2 2016, 17:42:40) Type "copyright", "credits" or "license" for more information. IPython 3.2.3 -- An enhanced Interactive Python.? -> Introduction and overview of IPython s features. %quickref -> Quick reference. help -> Python s own help system. object? -> Details about object, use object?? for extra details. In [1]: Maoying Wu (CBB) BI296-Lec05 Spring, 2017 7 / 44

Run under Jupyter Notebook Run jupyter notebook Maoying Wu (CBB) BI296-Lec05 Spring, 2017 8 / 44

Run Python Non-interactively ( 非交互式 ) Write script file hello.py #!/usr/bin/env python """ hello.py """ name = input("what is your name? ") print "Hello, {}!".format(name) Approach 1 chmod u+x hello.py./hello.py Approach 2 python hello.py Maoying Wu (CBB) BI296-Lec05 Spring, 2017 9 / 44

Next we will talk about... 1 Introduction 2 Data types 3 flow control 4 functions Maoying Wu (CBB) BI296-Lec05 Spring, 2017 10 / 44

Basic types ( 基本数据类型 ) Basic object types in Python: bool( 布尔型 ) The class of the two boolean constants True, False. int( 整型 ) Integer numbers: 1, -2,... long( 长整型 ) Integer numbers of arbitrary size float( 浮点型 ) Double precision floating-point numbers, e.g.: 3.1415, -1e-3. str( 字符串型 ) Text (strings of byte-size characters). list( 列表 ) Mutable list of Python objects dict( 字典 ) Key/value mapping,also named hash or associative array. The list and dict types are essential data structures, so we are covering them extensively afterwards. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 11 / 44

Use type() to find out the type 字符串 :str >>> type("hello, world!") <type str > 列表 :list >>> type([1,2,3]) <type list > 整数 :int 或 long >>> type(123) <type int > 浮点数 :float >>> type(12.3) <type float > 复数 :complex >>> type(1.0 + 2j) <type complex > 元组 :tuple >>> type((1,2,3)) <type tuple > 字典 :dict >>> type({ a :1, b :2}) <type dict > 集合 :set >>> type({1,2,3}) <type set > Maoying Wu (CBB) BI296-Lec05 Spring, 2017 12 / 44

String literals ( 字符串文本 ) Single and double quotes can be used interchangeably ( 单双引号等效 ): >>> "a string" == a string True You can use the single quotes inside double-quoted strings, and vice versa ( 单双引号相互包裹 ): >>> a = "Isn t it ok?" >>> b = "Yes", he said. Multi-line strings are enclosed by three single/duoble quotes ( 多行字串 ). >>> a = """This is a string,... that extends over more... than one line.... """ Maoying Wu (CBB) BI296-Lec05 Spring, 2017 13 / 44

Statements ( 语句 ), expressions ( 表达式 ) and operators ( 操作符 ) A statement is an instruction that Python can execute, such as x = 3 Operators are special symbols that represent computations, like addition, the values they operate on are called operands An expression is a combination of values, variable and operators x + 3 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 14 / 44

Operators ( 操作符 ) I All the usual unary ( 一元 ) and binary ( 二元 ) arithmetic operators ( 数学操作符 ) are defined in Python: +, -, *, /, ** (exponentiation), <<, >>, etc. Logical operators ( 逻辑操作符 ) are expressed using plain English words: and, or, not. Numerical and string comparison ( 数值 / 字符串比较操作符 ) also follows the usual notation: <, >, <=, ==,!=,... References: http://docs.python.org/library/stdtypes.html# boolean-operations-and-or-not http://docs.python.org/library/stdtypes.html# comparisons Maoying Wu (CBB) BI296-Lec05 Spring, 2017 15 / 44

Operators ( 操作符 ) II Some operators are defined for non-numeric types: >>> "S" + JTU SJTU Some supports operands of mixed type >>> "a" * 2 aa >>> 2 * "a" aa Some do not >>> "aaa" / 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: str and Maoying Wu (CBB) BI296-Lec05 Spring, 2017 16 / 44

Type casting ( 类型强制转换 ) >>> 12 + 1.30 13.30 >>> "a" + 10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate str and int objects >>> int( a ) + 10 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: a >>> int( a, 16) + 10 20 >>> "a" + str(10) a10 >>> float(12) / 5 2.4 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 17 / 44

Operators ( 操作符 ) III The mod operator % computes the remainder of integer division. >>> 9 % 2 1 >>> 9 % -2-1 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 18 / 44

Exercise Have a look at the following scripts, try to infer the rules: >>> 9 / 2 >>> 9 % 2 >>> -9 / 2 >>> -9 % 2 >>> 9 / -2 >>> 9 % -2 >>> -9 / -2 >>> -9 % -2 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 19 / 44

Operators ( 操作符 ) IV The % operator is also used for string formatting: >>> "This is slide %d of %d" % (11, 32) This is slide 11 of 32. >>> "We are %.1f %% done." % (100.0 * 11/32) We are 34.4% done. >>> "Today is %s %d, %d" % ( September, 11, 2012) Today is September 11, 2016 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 20 / 44

Operators ( 操作符 ) IV The % operator is also used for string formatting: >>> "This is slide %d of %d" % (11, 32) This is slide 11 of 32. >>> "We are %.1f %% done." % (100.0 * 11/32) We are 34.4% done. >>> "Today is %s %d, %d" % ( September, 11, 2012) Today is September 11, 2016 The %d in the left string is substituted with the base-10 representation of the integer number on the right side. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 20 / 44

Operators ( 操作符 ) IV The % operator is also used for string formatting: >>> "This is slide %d of %d" % (11, 32) This is slide 11 of 32. >>> "We are %.1f %% done." % (100.0 * 11/32) We are 34.4% done. >>> "Today is %s %d, %d" % ( September, 11, 2012) Today is September 11, 2016 The %f in the left string is substituted with the floating-point number on the right side; the number of digits to put after the comma is written before the f Maoying Wu (CBB) BI296-Lec05 Spring, 2017 20 / 44

Operators ( 操作符 ) IV The % operator is also used for string formatting: >>> "This is slide %d of %d" % (11, 32) This is slide 11 of 32. >>> "We are %.1f %% done." % (100.0 * 11/32) We are 34.4% done. >>> "Today is %s %d, %d" % ( September, 11, 2012) Today is September 11, 2016 The %s in the left string is substituted with the string on the right side. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 20 / 44

Integers ( 整数 ) 1, 2, -32, 45, 10000000000000000000 Operators for integers + - * / % ** Note: / uses integer division: 5 / 2 yields 2 But, if one of the operands is a float, the return value is a float: 5 / 2.0 yields 2.5 Note: Python automatically uses long integers for very large integers. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 21 / 44

Floats ( 浮点数 ) 1.23, 6.43, 1.0E-20 A floating point number approximates a real number. Note: only finite precision, and finite range (overflow)! Operators for floats + addition - subtraction * multiplication / division ** power Maoying Wu (CBB) BI296-Lec05 Spring, 2017 22 / 44

Booleans ( 布尔类型 ) True, False Boolean expressions: == equals: 5 == 5 yields True! = not equal: 5!= 5 yields False > greater than: 5 > 4 yields True >= greater than or equal: 5 >= 5 yields True is is the same object: 5 is 5 yields True Similarly, we have < and <=. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 23 / 44

Booleans ( 布尔类型 ) True, False Boolean expressions: == equals: 5 == 5 yields True! = not equal: 5!= 5 yields False > greater than: 5 > 4 yields True >= greater than or equal: 5 >= 5 yields True is is the same object: 5 is 5 yields True Similarly, we have < and <=. Logical operators: True and False yields False True or False yields True not True yields False Maoying Wu (CBB) BI296-Lec05 Spring, 2017 23 / 44

== vs. is We can use is compare two objects, to check whether they are pointing to the same stuff. >>> a = 100; b = 100 >>> a == b True >>> a is b True >>> id(a) == id(b) # memory address True >>> a = 257; b = 257 >>> a == b True >>> a is b False >>> id(a) == id(b) # memory address False Maoying Wu (CBB) BI296-Lec05 Spring, 2017 24 / 44

Next we will talk about... 1 Introduction 2 Data types 3 flow control 4 functions Maoying Wu (CBB) BI296-Lec05 Spring, 2017 25 / 44

Flow Control More control over your program Flow control statements allow you to do more complicated tasks. if for while Maoying Wu (CBB) BI296-Lec05 Spring, 2017 26 / 44

if statements Using if, we can execute part of a program conditional on some condition being true. if traffic_light == blue : drive() elif traffic_light == yellow : accellerate() else: freeze() Maoying Wu (CBB) BI296-Lec05 Spring, 2017 27 / 44

for loops 遍历 Very often, one wants to repeat some action. This can be achieved by a for loop for i in range(5): print i**2 Here, range(n) gives us a list with integers 0,..., n 1 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 28 / 44

while loops 条件成立则重复 When we not know how many iterations are needed, we can use while. i = 1 while i < 100: print i**2, i += i**2 # a += b is short for a = a + b # 1 4 36 1764 Maoying Wu (CBB) BI296-Lec05 Spring, 2017 29 / 44

continue 进入下一循环 continue continues with the next iteration of the smallest enclosing loop. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 30 / 44

continue 进入下一循环 continue continues with the next iteration of the smallest enclosing loop. for num in range(2, 10): if num % 2 == 0: print "Found an even number", num continue print "Found an odd number", num Maoying Wu (CBB) BI296-Lec05 Spring, 2017 30 / 44

break 跳出循环 The break statement allows us to jump out of the smallest enclosing for or while loop. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 31 / 44

break 跳出循环 The break statement allows us to jump out of the smallest enclosing for or while loop. Finding prime numbers: max_n = 10 for n in range(2, max_n): for x in range(2, n): if n % x == 0: # n divisible by x print n, equals, x, *, n/x break else: # executed if no break in for loop # loop fell through without finding a factor print n, is a prime number Maoying Wu (CBB) BI296-Lec05 Spring, 2017 31 / 44

pass 什么都不做 The pass statement does nothing, which can come in handy when you are working on something and want to implement some part of your code later. if traffic_light == green : pass # to implement else: freeze() Maoying Wu (CBB) BI296-Lec05 Spring, 2017 32 / 44

Next we will talk about... 1 Introduction 2 Data types 3 flow control 4 functions Maoying Wu (CBB) BI296-Lec05 Spring, 2017 33 / 44

How to define new functions The def statement starts a function def greet(name): definition. """ A friendly function. """ print ("Hello, " + name + "!") # the customized greeting greet("world") Maoying Wu (CBB) BI296-Lec05 Spring, 2017 34 / 44

Indentation is significant in def greet(name): Python: it is used to delimit blocks """ of code, like { and } in Java and A friendly function. C. """ print ("Hello, " + name + "!") # the customary greeting greet("world") Maoying Wu (CBB) BI296-Lec05 Spring, 2017 35 / 44

(This is a comment. It is ignored by def greet(name): Python, just like blank lines.) """ A friendly function. """ print ("Hello, " + name + "!") # the customary greeting greet("world") Maoying Wu (CBB) BI296-Lec05 Spring, 2017 36 / 44

def greet(name): """ A friendly function. """ print ("Hello, " + name + "!") # the customary greeting greet("world") This calls the function just defined. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 37 / 44

def greet(name): """ A friendly function. """ print ("Hello, " + name + "!") # the customary greeting greet("world") What is this? The answer in the next exercise! Maoying Wu (CBB) BI296-Lec05 Spring, 2017 38 / 44

Modules ( 模块 ) Not all functionality avaible comes automatically when starting python, and with good reasons. We can add extra functionality by importing modules ( 加载模块 ): >>> import math >>> math.pi 3.141592653589793 >>> from math import pow >>> pow(3,4) 81.0 Useful modules ( 常用模块列表 ): math, string, random, and as we will see later numpy, scipy, matplotlib, pandas, scikit-learn. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 39 / 44

Important functions for module >>> import sys >>> dir(sys) [ displayhook, doc, excepthook, name package, stderr, stdin, stdout, _clear_type_cache, _current_frames, _getframe, _mercurial, api_version, argv, builtin_module_names byteorder, call_tracing, callstats, copyright, displayhook, dont_write_bytecode, exc_clear, exc_info, exc_type, excepthook, exec_prefix, executable, exit, flags, float_info, float_repr_style, getcheckinterval, getdefaultencoding, getdlopenflags, getfilesystemencoding, getprofile, getrecursionlimit, getrefcount, getsizeof, gettrace, hexversion, long_info, maxint, maxsize, maxunicode, meta_path, modules, path, path_hooks, path_importer_cache, platform, prefix, ps1, ps2, py3kwarning, setcheckinterval, setdlopenflags, setprofile, setrecursionlimit, settrace, Maoying Wu (CBB) stderr, stdin, BI296-Lec05 stdout, subversion, Spring, 2017 40 / 44

Exercise A: Type and run the code on the previous page at the interactive prompt. (Type indentation spaces, too!) What does help(greet) print? What s the result of evaluating the function greet("world")? Exercise B: Type the same code in a file named hello.py, then type import hello at the interactive prompt. What happens? Type import hello again; what happens? Exercise C: At the terminal, type python hello.py. What happens? Type it again; what happens? Maoying Wu (CBB) BI296-Lec05 Spring, 2017 41 / 44

Modules, I The import statement reads a.py file, executes it, and makes its contents available to the current program. >>> import hello Hello, world! Modules are only read once, no matter how many times an import statement is issued. >>> import hello Hello, world! >>> import hello >>> import hello Maoying Wu (CBB) BI296-Lec05 Spring, 2017 42 / 44

Modules, II Modules are namespaces: functions and variables defined in a module must be prefixed with the module name when used in other modules: >>> hello.greet("bob") Hello, Bob! To import definitions into the current namespace, use the from x import y form: >>> from hello import greet >>> greet("bob") Hello, Bob! Maoying Wu (CBB) BI296-Lec05 Spring, 2017 43 / 44

The return statement def double(x): return x+x double(3) == 6 def double(x): return x+x # the following line # is never exec d print( Hello ) The result of a function evaluation is set by the return statement. If no return is present, the function returns the special value None. After executing return the control flow leaves the function. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 44 / 44

Exercise Generate two random variables between 1 and 100 using the module random Prompt the users to compute the product of the above two integers. Check whether the result is correct. Repeat the above process 10 times. Compute the final score, with each question scoring 10. Maoying Wu (CBB) BI296-Lec05 Spring, 2017 45 / 44