Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Similar documents
CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

University of Texas at Arlington, TX, USA

Crash Dive into Python

Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values.

Senthil Kumaran S

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Introduction to Bioinformatics

List of squares. Program to generate a list containing squares of n integers starting from 0. list. Example. n = 12

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

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

Python - Variable Types. John R. Woodward

CIS192: Python Programming

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

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

Table of Contents EVALUATION COPY

Object Oriented Programming. Feb 2015

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

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

Pace University. Fundamental Concepts of CS121 1

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Java Bytecode (binary file)

Data Structures. Lists, Tuples, Sets, Dictionaries

Collections. Lists, Tuples, Sets, Dictionaries

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

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

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

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

Pythonic Coding Style. C-START Python PD Workshop

Lecture 2 Tao Wang 1

Python Review IPRE

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

All programs can be represented in terms of sequence, selection and iteration.

Decision Making in C

Some material adapted from Upenn cmpe391 slides and other sources

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Lists, loops and decisions

LECTURE 3 Python Basics Part 2

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

[301] JSON. Tyler Caraza-Harter

Programming Environments

CS 234 Python Review Part 2

Chapter 2 Getting Started with Python

MATLAB 1. Jeff Freymueller September 24, 2009

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

Accelerating Information Technology Innovation

Introduction to Python - Part I CNV Lab

Python. Karin Lagesen.

Python. Olmo S. Zavala R. Python Data Structures. Lists Tuples Dictionaries. Center of Atmospheric Sciences, UNAM. August 24, 2016

Control Structures. CIS 118 Intro to LINUX

Visualize ComplexCities

MACRO NOTES DOCUMENTATION

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

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

The Practice of Computing Using PYTHON

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

For strings (and tuples, when we get to them), its easiest to think of them like primitives directly stored in the variable table.

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Python in 10 (50) minutes

SOFT 161. Class Meeting 1.6

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

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

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

C++ for Python Programmers

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

Full file at

Introduction to Python

Key Differences Between Python and Java

Python: common syntax

Accelerating Information Technology Innovation

Typescript on LLVM Language Reference Manual

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

Java+- Language Reference Manual

LECTURE 1. Getting Started with Python

A Func'onal Introduc'on. COS 326 David Walker Princeton University

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

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

Python Intro GIS Week 1. Jake K. Carr

Language Reference Manual

Introduction to Python

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Webinar Series. Introduction To Python For Data Analysis March 19, With Interactive Brokers

This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules.

Introduction to Python Part I

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Introduc)on. Tristan Naumann. Sahar Hasan. Steve Hehl. Brian Lu

AI Programming CS S-02 Python

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Writing a Fraction Class

Python Review IPRE

JavaScript Basics. The Big Picture

The SPL Programming Language Reference Manual

Transcription:

Introduction to Python Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Things Covered What is Python? How to access Python environment? Fundamental elements in Python Variables (assignment, comparison, display, ) Basic data structure (lists, dicts and tuples) Loops Decision making Func5ons File Input and output Python program Resources aoer this course

What is Python? Python is a widely used High level - higher level of abstrac5on from machine language. General-purpose - designed to be used for wri5ng sooware in a wide variety of applica5on domains. Interpreted - directly executes without compiling it into a machine language. Dynamic executes many common programming behaviors in run5me while sta5c programming languages perform during compila5on 5me. programming language. It emphasizes code readability, simple syntax. It uses whitespace indenta5on, rather than curly braces or keywords, to delimit blocks. It is cross-pla[orm. Python was developed by Guido van Rossum in the late eigh5es and early nine5es at the Na5onal Research Ins5tute for Mathema5cs and Computer Science in the Netherlands.

How to access Python Download and install Enthought canopy python (GT login) h^ps://www.enthought.com/products/canopy/ Web-based python tool pythonfiddle.com Self installed and managed Python tool sets (for advance user) h^ps://www.python.org/downloads/ Exis5ng PACE user, simply login to PACE system do: module load python or follow the instruc5on at: h^p://pace.gatech.edu/how-do-i-install-my-python-package Download course example files at: h^p://pace.gatech.edu/sites/ default/files/python101.zip This Course is using Python 2.7

Built-in Datatypes Basic Types int, float, long, complex, boolean Strings Collec5ons Dic5onary (Key:Value pairs) List: collec5on of objects (any python object) Tuple: similar to lists, but unmutable Everything is an object Has its own methods to manipulate it

Variables Strong Typed (string, numbers,.), name must start with a le^er Create variables =, delete variables del <v> Logical Operators ( and, or, not, ) Comparison Operators (==, <>,!=,>=,<=,>,<) Arithme5c Operators (+,-,*,/,%,**,//) Assignment Operators (=,+=,-=,*=,/=%=,**=) Membership Operators (in, not in) Display informa5on (print) Examples 01.py

Mutable vs Immutable Immutable Data cannot be modified in place E.g. string/number/tuple Can only replace the old value with a completely new value Mutable data can be modified in place E.g. list/dict Can be modified aoer they have been created, such as changing the individual elements, appending new elements, etc;

Basic Data Structures (List) Dynamical storage structure anything can be put into a list Items are separated by commas and enclosed with square brackets [] Create a new list mylist=list();mylist.append(oneitem) mylist=[item1,item2,item3, ] mylist=list([item1,item2,item3], )

Basic Data Structure (List) (Cont.) New item is added to the end of list by append() Remove item from the end of the list by pop() Remove one item from the list by del(item) List s index starts from 0, item can be retrieved as mylist[ind] Item can be retrieved from the end of list as mylist[-n] (-1 means the end of list) Find the index of one item by mylist.index(item) Find the length of the list by len(mylist) All items are case sensi5ve Example: 02.py

Basic Data Structure (Tuple) Similar to list, but items and sizes cannot be changed (read only list) enclosed in parentheses () Created as mytuple=tuple([item1,item2,item3 ]) mytuple=(item1,item2,item3, ) mytuple=item1,item2,item3, Access as mytuple[ind] Big difference from list is that tuples are IMMUTABLE while lists are MUTABLE. Example: 03.py

Basic Data Structure (Dict) Similar to list, items and sizes can be changed Items are separated by commas and enclosed with curly brackets {} Storing the key:value pairs Created as: a comma-separated list of key: value pairs within braces mydict={ key1 :value1, key2 :value2, } mydict=dict(), mydict[ key1 ]=value1,mydict[ key2 ]=value2, Search based on key instead of index in list unordered - make searching efficient (implemented as a hash table) It is MUTABLE, mydict[ key ]=newvalue and mydict[ newkey ]=newvalue Example: 04.py

Loops Able to iterate over the items of any sequence, and do repeated things There are for loop and while loop For Loop Syntax: (be sure about the indenta5on on second line) for itera/ng_var in sequence: Steps: statement(s) Itera/ng_var is the items in the sequence The colon : at the end of line tells Python that everything NESTED below it is part of the loop The statements(s) that are part of the loop must be indented in order for Python to count it as part of the loop

Loops (Cont.) While loop Syntax: (be sure about indenta5on on the second line) Steps: while expression: statement(s) statements may be a single statement or a block of statements When expression becomes false, program passes to the line immediately following the loop Examples 05.py

Decision Making Python assumes any non-zero and non-null values as TRUE, so any zero or null values are treated as FALSE Syntax: if expression: else: statement(s) otherstatement(s) If the boolean expression evaluates to TRUE, then the block of statement(s) nested within if statement is executed. If the boolean expression evaluates to FALSE, then the block of otherstatement(s) nested within else statement is executed. if and else are keywords Example 06.py

Functions Func5ons are ways to package the code so that it is easier to reuse Defining a func5on def myfunct(param1, param2, ): Steps: statement(s) def : keyword to begin the declara5on of a func5on myfunct : func5on name param1, param2, : comma separated arguments in the parenthesis statement(s) are indented no need to specify datatype of param1, param2, Call a func5on myfunct(v1, v2, ) --- v1 and v2 are the list of input values

Functions (Cont.) Define a func5on with return values: def myfunctwithreturn(param1, param2, ): Steps: statement(s) return rvaule1,rvalue2, return : keyword to tell func5on to pass the result to calling func5on no need to specify rvalues datatype Call func5on (localvalue1, localvalue2, ) = myfunctwithreturn(param1, param2, ) Example: 07.py

File Input and Output Directly read from standard input (keyboard) myinput = raw_input("enter your input: "); print "Received input is : ", myinput myinput is string type, you may need to convert string to other datatype explicitly and can be done as (int)myinput Read from file reading_file=open( /full/path/inpu[ile", "r") open opera5on takes two arguments, the first one is the loca5on of the file, the second one is the mode for opening, e.g. ( r read, w write) aoer file is opened, you have one file object reading_file Example 08.py

Python Program Real python code to put everything in a self contained file Include : a main func5on is defined as : def main(): Calling it as: if name == " main ": main() Example: run as python 09.py inpu[ile2 within handson directory.

For Self-Taught intermediate Python Lynda online courses h^p://lynda.gatech.edu/, search for python (need GT account) SoOware Carpentry Programming with Python (free to public) h^p://swcarpentry.github.io/python-novice-inflamma5on/ index.html