Introducing Python CS 4320, SPRING 2015. Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.

Slides:



Advertisements
Similar presentations
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

Container Types in Python
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Introduction to Python. Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Python Control of Flow.
Introduction to Python Guido van Rossum Director of PythonLabs at Zope Corporation
Lists in Python.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Python September 26, /10/ Bioinformatics Languages Low-level, compiled languages: C, C++, Java… Pros: performance Cons:
Input, Output, and Processing
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
Python Variable Types.
Python: Experiencing IDLE, writing simple programs
Python Let’s get started!.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Pamela Moore & Zenia Bahorski
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Engineering Innovation Center
Introduction to Strings
Introduction to Strings
Introduction to Python
Python Primer 2: Functions and Control Flow
CHAPTER THREE Sequences.
Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
String and Lists Dr. José M. Reyes Álamo.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Primer 1: Types and Operators
Introduction to Strings
Introduction to Computer Science
Introduction to Strings
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Learning Python 5th Edition
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introducing Python CS 4320, SPRING 2015

Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections and subsections: ◦Section 1: Whetting your Appetite ◦Section 3: An Informal Introduction to Python ◦Subsections , 4.5 and 4.6: More Control Flow ◦Subsubsections : More on defining functions ◦Subsubsections : More on Lists ◦Subsection 5.3: Tuples ◦Section 6, 6.1, 6.1.1, 6.4: Modules You are encouraged to read more than the minimum and, of course, to try out the examples presented in the tutorial 1/7/ CS 4320, SPRING 2015

Sources of Information The most important source about Python is the documentation for the Python Standard LibraryPython Standard Library ◦As the Python site says: ‘keep this under your pillow’ The Python documentation page points to other good sourcesPython documentation page 1/7/2015CS 4320, SPRING

History Developed by Guido van Rossum in the late 1980’s Now developed to version 3.x Python is multi-paradigm: imperative and OO and some functional 1/7/2015CS 4320, SPRING

Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is syntactically important ◦Blocks of code, as inside an ‘if’, are indicated by indenting ◦Code that is sequential must not be indented Statement continuation ◦Statements normally finish at the end of a line ◦A statement will continue to further lines if there are parentheses or braces still not matched ◦A single backslash, \, at the end of a line will also continue the statement ◦Note: no characters, including spaces, are allowed after \ in this case Summary: In Python, white-space is important 1/7/2015CS 4320, SPRING

Global Level Python allows definitions and code at the global level That is, not everything has to be inside a class definition The famous ‘hello world’ program can be one line: ◦ print(‘hello world!’) This also means that variables can have a global scope: seen by every part of the code. Be careful, global variables can make very difficult code Many important pre-defined functions are free (Built-in Functions Reference)Built-in Functions Reference 1/7/2015CS 4320, SPRING

First Program Creating a project Creating a file Running from the IDE Running a script from the command line ◦The interpreter definition line #! ◦Change the mode of the file to executable Running the Python interpreter interactively 1/7/2015CS 4320, SPRING

Printing The print function is used to print data to standard output Arguments must be enclosed in parentheses (a major difference from Python 2) Arguments are separated by a space when printed ◦So, print(“x is”, x) will leave a space between the label and the value The print function inserts a new line after printing the arguments ◦You can change this by using the keyword argument end=… 1/7/2015CS 4320, SPRING

Basic Data Types Numeric Strings Lists Tuples Dictionaries 1/7/2015CS 4320, SPRING

Numeric Data Float and Integer are subsumed under a single type Integer arithmetic is arbitrary precision When doing division ◦/ gives a float result ◦// gives an integer result 1/7/2015CS 4320, SPRING

Strings Strings are, as in Java, immutable sequences of characters ◦The global len function will return the length of a string String literals can be delimited by single or double quotes, these must be on one line Strings can be delimited by ‘triple quotes’ (either ‘’’ or “””) ◦This form can be multi-line 1/7/2015CS 4320, SPRING

Lists Lists are ordered, indexed sequences of values Lists are not typed, so pretty much any values can be mixed into a list Square brackets are used to delimit a literal list in code Indexing ◦Indexes start at 0 ◦Negative indexes count from the end of the list, so s[-1] is the last element in list s ◦s[i] is the item at index I ◦s[k:j] is the sublist of items from k to j (not including j) ◦s[k:] is the sublist from k to the end ◦s[i:j:k] is the sublist from I to j in steps of k 1/7/2015CS 4320, SPRING

List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds x to the end of list s s.pop(i) removes and returns the element at index I ◦s.pop() is the same as s.pop(-1) 1/7/2015CS 4320, SPRING

Tuples Tuples are immutable lists As literals, tuples are given by a comma separated list of values surrounded by parentheses ◦(1,2,3) Beware! ◦(1) is not a tuple, it is simply the integer 1 ◦(1,) is a tuple with one element, the integer 1 Tuples turn up in some places, especially as the return values from some functions. ◦Just be aware that tuples cannot be modified 1/7/2015CS 4320, SPRING

Dictionaries Also known as ‘hash maps’, ‘associative memory’, and ‘maps’ A dictionary associates keys to values A literal dictionary uses curly braces with pairs ‘key:value’ 1/7/2015CS 4320, SPRING

Selection The semantics of the if statement are familiar, however the syntax is different from Java The body of each part of the ‘if’ must be indented. Four spaces is pretty standard. ◦The indentation must be consistent throughout the body 1/7/2015CS 4320, SPRING If x < 3: print(‘small’) elif x < 5: print(‘medium’) else: print(‘large’)

Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the list s 1/7/2015CS 4320, SPRING for x in s: print(x)

Ranges For stepping through ranges of integers, the range function is available range(10) is the numbers from 0 to 9, inclusive range(5,10) is the numbers from 5 to 9, inclusive range(5,10,2) is the numbers 5, 7 and 9 1/7/2015CS 4320, SPRING