Introduction To Python - Hprc.tamu.edu

1y ago
8 Views
2 Downloads
972.80 KB
41 Pages
Last View : 22d ago
Last Download : 2m ago
Upload by : Luis Waller
Transcription

Introduction to PythonYang LiuAssociate Research ScientistHigh Performance Research ComputingTexas A&M UniversityJune 6, 2017Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Why PythonQuick for a prototype: General purpose high-level programming language (file, network, web, database,GUI, etc.)Abundant packages (numpy, scipy, biopython, pandas, ntlk, tensorflow, etc.)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Short HistoryCreated by Guido Van Rossum in early 1990s. Python conceived – last 1980s Python 1.0 – 1994 Python 2.0 – 2000 2.7 – 2010 (last version of Python 2)Python 3.0 – 2008 (if no legacy issue, use python 3. In this short course, we will learnpython 3). 3.5 – 201506/05/17Texas A&M University3High Performance Research Computing – http://hprc.tamu.edu

Access Python on AdaRecall: “ssh your net id@ada.tamu.edu“ to login to ada cluster# load Anaconda module# list python version# list which python to use06/05/17Texas A&M University4High Performance Research Computing – http://hprc.tamu.edu

Data Type: NumberA number can be an integer, a floating pointer number, or a boolean. (Everything inPython is an object)# integer# floating point number# boolean# automatic conversion for number operations# everything is an object in python06/05/17Texas A&M University5High Performance Research Computing – http://hprc.tamu.edu

Data Type: StringA string is a sequence of characters quoted by double/single/triple quotes.#string in singlequote#string in doublequote (with escapedcharacter ‘\n’)# string in triplequote whichpreserve its formatTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Data Type: ListA list is a collection of items (elements) which are enclosed by ‘[‘ and ‘]’.# define a list# print (function)a list# access a listelement# operator “ ”concatenate twolistsTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

SlicingLet x be a sequence (string, list, etc). Then a slicing x[start:end:step] (step is positive) is A sequence of elements: x[start], x[start step], x[start 2 * step], , x[start m *step] where# slicing definition is start m * step end anddifferent when step isnegative (not covered in start (m 1) * step endthis course)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

List ComprehensionList comprehension is to create a new list from an old list by selecting elements from old list with/without conditions, And applying operations to those selected elements.# slicing without condition# slicing with conditionExercise:Use list comprehension to create a new list: its elements are the double ofthose positive integers from a given list [-1, 5, 32, -98, 3912, -238, 72, 666].Answer:[10, 64, 7824, 144, 1332]Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Flow Control: If ElseWhen the if condition is true, statements for the ‘if’ are executed. Otherwise, statementsfor the ‘else’ are executed.# do not forgot the colon “:”# condition “x %2 0” is true, the printstatement following ‘if’ is executed# condition “x %2 0” is false, the printstatement following ‘else’ is executedTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Indention in PythonPython use indentation (white spaces) to group program statements (C/C uses ‘{‘ and ‘}’). Adjacent statements with the same indentations belong to the same group.# The two print statements have the sameindention. So they both belong to the ifstatement# operator ‘%’ is an modular operation: a% b is the remainder of a/b. For example:10 % 3 1 since 10 3* 3 1, that is, theremainder of 10 / 3 is 1.# ‘ ’ is a comparison operator. ‘a b’ istrue is a is equal to b, and false otherwise.Note it is different to the assignmentoperator ‘ ’.Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Flow Control: ForStatements of ‘for’ loop are executed for each element in the sequence as specified in the‘for’ loop.# do not forgot thecolon “:”Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Flow Control: WhileWhile statement continues to execute the statements belong to this loop until thecondition is not true# reminder of thecolon “:” andindentionTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Flow Control: BreakBreak statement in a loop exits from that loop# ‘break’ statementexits the while loopwhen it is executedExercise:What will be the output if there is no ‘break’ statement in the example?Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Example: Right Triangle# print() without ‘end ’ in it, it addsnew line character ‘\n’ in the end ofits output# print() with ‘end x‘ in it, it addscharacter x in the end of its outputTexas A&M UniversityExercise:What will be the output if there is no‘j j 1’ statement in the exampleabove?High Performance Research Computing – http://hprc.tamu.edu

Problem: Finding Factors (1)Write a python program to find the factors of 1000: 1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100,125, 200, 250, 500. (range(1,1000) returns a list of integer from 1 to 1000: no 0) Using for loop Using while loop Using list comprehensionTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Problem Solution: FindingFactors (1)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Python ScriptsA python script is a file with python statements.# ‘python scrip name’executes the pythonprogram in filescript nameExercise:What will happen if run ‘module purge’ before executing the script?Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Python Executable ScriptsA python executable script is a python script with executable bit set.# “#!/bin/envpython” is to usepython found inenvironment (env)# “chmod x” setsthe file to be anexecutable file# no need of‘python’ in front ofthe script name toexecute the scriptExercise:What will happen if run ‘module purge’ before executing the script?Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Edit Python Scripts on AdaDepends on which operating systems you use, there are various ways to connect to Adato edit python scripts on Ada. “ssh” to ada and use an editor (vi, emacs, etc) “ssh -X” to ada and use gedit (slow sometimes) Edit python scripts on your computer (windows, mac, etc) and copy to ada. Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Built-in FunctionsThe Python interpreter provides a number of built-in ons.html# function ‘sorted’ sorts all elements (in a list )# function ‘abs’ returns the absolute value of a number# funciton ‘int’ convert its parameter to integer# function ‘sum’ returns the sum of all elementsTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

User Defined FunctionsA user may define a function other than just using built-in functions# ‘def’ defines afunction. Do notforgot the colon “:”# ‘n’ is theargument/parameter for function factor# reminder ofindentionExercise:What will happen if range(1,n) is changed to range(n)?Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

File I/OA python program can read from or write to a file.Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

File I/O: Automatically CloseA file is automatically closed after ‘with’ statement is finished if that file is opened in the‘with’ statement.Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Problem: Finding Factors (2)Write a program (script) factors.py which reads input from input file number.txt of positiveintegers (one per line) and write the factors of those integers to output filenumber factors.txt (all factors for an integer are in the same line). Your program shouldhave a function finding factors which takes an integer as a parameter and return a list ofintegers.Example number.txt:61025Texas A&M UniversityExample number factor.txt:12361 2 5 101 5 25High Performance Research Computing – http://hprc.tamu.edu

Problem Solution: FindingFactors (2)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

ModuleA module is a python script. We use the terminology ‘module’ when importing a pythonscript (or library)# thefunction factor.py isnot in current directoryand it includesfunction factor().# failed to call thefunction factor() whichis in a program not atcurrent directoryTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Module: ImportWe can import a module which is found in PYTHONPATH# PYTHONPATH sets the paths/directories to search for python modules# Once we add the directory where the function-factor is to PYTHONPATH (by‘export PYTHONPATH .’), the factor function can be found and is available(with module name is a prefix to the function name)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Module: Import From“Import” make all contents from the imported module available. “Import From” can justimport needed contents from the module to import.# only import‘factor’ from modulefunction factor# module name isnot available (notimported)Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Problem: Finding Factors (3)Go to a directory different to where your solution to problem 2 is. Import the modulefactors.py and call its function to return the factors for integer 2018.Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Object Oriented ProgrammingStudentClass: IdDepartmentset department()display()Student: student1 Student:student2Id 101Department 'Computer'Id 125Department 'Biology'set department()display()set department()display()06/05/17Texas A&M UniversityInclude bothdata andmethods(functions)Used as aprototype tocreateinstancesInstance: Has own data31High Performance Research Computing – http://hprc.tamu.edu

Class: Example# Class constructor “ init “executed when an instance is created# instance student 1 has its own data# instance student 2 has its own data06/05/17Texas A&M University32High Performance Research Computing – http://hprc.tamu.edu

Class VariableA class variable is shared among all instances: one instance change a class variable, theother instances see the change# ‘count’ is a class variable# Note that the ‘Student Count’ in outputfrom the first student 1.display() is differentto that from the second student 1.display().The change is caused by the creation ofstduent 2.06/05/17Texas A&M University33High Performance Research Computing – http://hprc.tamu.edu

Class InheritanceA class can inherit the data andfunctions in its parent class. Butit can also override its parentfunctions and add new data# Graduate Student inheritsfrom its parent Student# Child class Graduate studentcan have new function‘set advisor’# Child class Graduate Studentoverride its parent function‘display’06/05/17Texas A&M University34High Performance Research Computing – http://hprc.tamu.edu

MatplotlibMatplotlib is a Python 2D plotting library. It produces publication quality figures supports different Python versions.matplotlib 1.5 supports Python 2.7, 3.4, and 3.5matplotlib 1.4 supports Python 2.6, 2.7, 3.3, and 3.4matplotlib 1.3 supports Python 2.6, 2.7, 3.2, and 3.3matplotlib 1.2 supports Python 2.6, 2.7, and 3.1matplotlib 1.1 supports Python 2.4 to 2.7Texas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Matplotlib: pyplotMatplotlib.pyplot is a module with functions to make figures like MATLAB# 10 random points areconnected via linesTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Matplotlib: Marker# all 10 points havemarker of red dot# first 5 points havemarker of white crossTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Matplotlib: Lengend# legend shows themeaning of twodifferent markersTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Parallel Processing Multithreading Multiple threads sharing memory Great for I/O-bound applicationsMultiporcessing Multiple processes Good for CPU-bound applicationsMpi4py mpi for pythonGreat for CPU-bound applications06/05/17Texas A&M University39High Performance Research Computing – http://hprc.tamu.edu

Other Python ModulesModule spider Python Python/2.7.12-intel-2017A Python/2.7.12-iomkl-2016D Python/2.7.12-2017.0.035 Python/3.4.3-intel-2015B Python/3.5.1-foss-2016a Python/3.5.1-intel-2016a Python/3.5.2-foss-2016b Python/3.5.2-intel-2016a Python/3.5.2-intel-2016b Python/3.5.2-2017.0.035 and more.# If you need python with particularversion/compiler, those modules areavailable for you to select.Otherwise, anaconda and its virtualenvironments are easier to use.06/05/17Texas A&M University40High Performance Research Computing – http://hprc.tamu.edu

Upcoming Schedule (HPRCResearch Week) Tuesday Nvidia Deep Learning Institute lecture, 1:15 – 3:30 Data Literacy and Data Management, 3:45 – 4:45Wednesday Keynote talk: NSF’s computing and Information Science andEngineering Directorate (CISE) Research and CyberinfrastructurePriorities, 10:00 – 11:00Matlab Workshop on Data Analytics, Machine Learning, and CodeOptimization, 1:00 – 4:30More on Thursday and /rcompweek/home/agendaTexas A&M UniversityHigh Performance Research Computing – http://hprc.tamu.edu

Texas A&M University High Performance Research Computing - http://hprc.tamu.edu Introduction to Python Yang Liu Associate Research Scientist

Related Documents:

Silver Galaxy Try Galaxy at usegalaxy.org to see if it appropriate for your project Getting Access to HPRC Silver Galaxy Available to Texas A&M students, staff and faculty with a NetID and an HPRC account Apply for an HPRC account first https://hprc.tamu.edu/apply Then send an email request for a Silver Galaxy account

Texas A&M University High Performance Research Computing – https://hprc.tamu.edu Using Jupyter Notebook on HPRC Portal Using Jupyter Notebook-Basic Python Exercise 1 Create a new directory under pip_envs Create a new notebook inside it. Save the file. Print a message- Welcome to HPRC 16

Mechanical Engineering Office Building Suite 200 979.845.1252 mechanical-undergradprogram@tamu.edu Undergraduate Program Director Ms. Heather Lewis, hlewis@tamu.edu Academic Advisors Ms. Jennifer Desai, jdesai@tamu.edu Ms. Madison Heyman, mad97hey@tamu.edu Ms. Regina Muir, rmuir@tamu.edu Dr. Ashley Schmitt, schmittae@tamu.edu

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

EVL and Calit2 78.33 Calit2 and TAMU 105.44 Table 2 presents the average network latency for round-trip using simple unix command ping between TAMU and EVL, between EVL and Calit2, and between Calit2 and TAMU. The latency between TAMU and Calit2 is almost the sum of the latency between TAMU and EVL and the latency between EVL and Calit2.

with the requirements of ISO 14001:2015? 4.4 14 Has your organization has considered the knowledge and information obtained by 4.1 and 4.2 when implementing and operating it EMS? Insert your company’s name or logo. ISO 14001:2015 Audit Checklist System & Process Compliance Auditing www.iso-9001-checklist.co.uk Page 6 of 41 Audit Findings Summary Manually transfer the audit findings from the .