Introduction to Python

Slides:



Advertisements
Similar presentations
Introduction to Python
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
CSC 9010: Natural Language Processing
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Python From the book “Think Python”
Outline  Why should we use Python?  How to start the interpreter on a Mac?  Working with Strings.  Receiving parameters from the command line.  Receiving.
Intro Python: Variables, Indexing, Numbers, Strings.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Python Fundamentals: Hello World! Eric Shook Department of Geography Kent State University.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
NWEN241 – Systems Programming
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
MATLAB – More Script Files
Introduction to Python
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lecture 2 Python Basics.
Introduction to Python
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Introduction to Python
Introduction to Python
Lecture 1 Introduction to Python Programming
Chapter 4 MATLAB Programming
PROGRAMMING IN HASKELL
Chapter 2 First Java Programs
Introduction to Programming the WWW I
JavaScript Syntax and Semantics
Tutorial Lecture for EE562 Artificial Intelligence for Engineers
PROGRAMMING IN HASKELL
CompSci 230 Software Construction
Data Analysis using Python-I
1 Python Lab #1 Intro to Python Adriane Huber Debbie Bartlett.
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Perl for Bioinformatics
Conditionals (if-then-else)
Automating reports with Python
Introduction to Python
PYTHON Varun Jain & Senior Software Engineer
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
MATH 493 Introduction to MATLAB
Lecture 2 Python Programming & Data Types
An Introduction to Python
Learning Outcomes –Lesson 4
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Java Intro.
Variables, Data Types & Math
Lecture 2 Python Programming & Data Types
15-110: Principles of Computing
Introduction to Python
Variables, Data Types & Math
Beginning Python Programming
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Chapter 1: Programming Basics, Python History and Program Components
Input and Output Python3 Beginner #3.
Computer Programming-1 CSC 111
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Learning Python 5th Edition
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

Introduction to Python

Why Python? Python is C/C++ is much faster but much harder to use. easy to learn, relatively fast, object-oriented, strongly typed, widely used, and portable. C/C++ is much faster but much harder to use. Java is about as fast and slightly harder to use. Perl is slower, is as easy to use, but is not strongly typed.

Getting started Install Python http://www.python.org/download/ Install a syntax-highlighting text editor: Atom PyCharm Notepad++ Sublime

Your first Python session Start a terminal session. Type “python” This should start the Python interpreter > python Python 2.7.10 (default, Oct 23 2015, 19:19:21)  [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>  >>> print “hello, world!” hello, world!

The interpreter Try printing various things Leave off the quotation marks. Print numbers, letters and combinations. Print two things, with a comma between. Enter a mathematical formula. Leave off the word “print”. The interpreter allows you to try things out interactively and quickly. Use the interpreter to test syntax, or to try commands that you’re not sure will work when you run your program.

Your first program In your terminal, Ctrl-D out of python. Type “pwd” to find your current working directory. Open an editor. Create a file containing one line: print “hello, world!” Be sure that you end the line with a carriage return, and do not use “smart quotes.” Save the file in text format as “hello.py” in your current working directory. In your terminal, type “python hello.py” > python hello.py hello, world!

Objects and types We use the term object to refer to any entity in a python program. Every object has an associated type, which determines the properties of the object. Python defines six types of built-in objects: Number 10 String “hello” List [1, 17, 44] Tuple (4, 5) Dictionary {‘food’ : ‘something you eat’, ‘lobster’ : ‘an edible, undersea arthropod’} Files Each type of object has its own properties, which we will learn about in the next several weeks. It is also possible to define your own types, comprised of combinations of the six base types.

Literals and variables A variable is simply another name for an object. For example, we can assign the name “pi” to the object 3.14159, as follows: >>> pi = 3.14159 >>> print pi 3.14159 When we write out the object directly, it is a literal, as opposed to when we refer to it by its variable name.

The assignment operator >>> pi = 3.14159 The '=' means assign the value 3.14159 to the variable pi (it does NOT assert that pi equals 3.14159). >>> print pi 3.14159 >>> pi = -7.2 -7.2

The “import” command Many python functions are only available via “packages” that must be imported. >>> print log(10) Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'log' is not defined >>> import math >>> print math.log(10) 2.30258509299

The command line To get information into a program, we will typically use the command line. The command line is the text you enter after the word “python” when you run a program. import sys print "hello, world!" print sys.argv[0] print sys.argv[1] The zeroth argument is the name of the program file. Arguments larger than zero are subsequent elements of the command line.

Sample problem #1 Write a program called “print-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints them on the same line with a colon between. > python print-two-args.py hello world hello : world

Solution #1 import sys arg1 = sys.argv[1] arg2 = sys.argv[2] print arg1, ":", arg2

Sample problem #2 Write a program called “add-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints their sum. Hint: To read an argument as a number, use the syntax “arg1 = float(sys.argv[1])” > python add-two-args.py 1 2 3.0

Solution #2 import sys arg1 = float(sys.argv[1]) print arg1 + arg2

Reading Chapter 1-2 of Think Python (1st edition) by Allen B. Downey.

One-minute response At the end of each class Write for about one minute. Provide feedback about the class. Was part of the lecture unclear? What did you like about the class? Do you have unanswered questions? Sign your name I will begin the next class by responding to the one-minute responses