Introduction To Python

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Title An Introduction to Python. What is Python exactly? Python is a modern rapid development language. Code is very clean and easy to read. Emphasizes.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Programming for Linguists An Introduction to Python 24/11/2011.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Input, Output, and Processing
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 >>> while b < 10:... print b... a, b = b, a+b WHILE.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Python The tutorial
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
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!.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Introduction to Programming
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Chapter 1: Introduction to computers and C++ Programming
Introduction to Python
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to C Programming
CSCE 590 Web Scraping Topics Readings: Overview – Web scraping
Introduction to Strings
Imperative Programming
Introduction to Strings
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 2 - Introduction to C Programming
An Introduction to Python
4. sequence data type Rocky K. C. Chang 16 September 2018
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Programming
CSCE 590 Web Scraping Topics Readings: Overview – Web scraping
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Introduction to Strings
Introduction to Java Applications
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
By Ryan Christen Errors and Exceptions.
Introduction to Strings
Lecture 7: Python’s Built-in Types and Basic Statements
Topics Introduction to File Input and Output
Introduction to Strings
Presentation transcript:

Introduction To Python Sarah Farley

What is Python? High level language Extensible No compilation or linking

Interpreter Type python on the command line to start the interpreter python -c command [arg] is an alternative Exit by ctl- d or quit() >>> Is the prompt for next command … indicates a continuation Comments are indicated with #

Python as a Calculator +, -, *, / act as you would expect them to >>> (50-5*6)/4 5 >>> 2+2 4 >>> 7/-3 -3

Assigning Variables = is used to assign variables >>> width = 20 >>> height = 5*9 >>> width * height 900 Values can be assigned simultaneously >>> x = y = z = 0

Floating Numbers When working with mixed operators, integers are turned into floats >>> 3 * 3.75 / 1.5 7.5 >>> 7.0 / 2 3.5

Strings Strings can be declared with ‘ or “. >>> 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> "\"Yes,\" he said." >>> '"Isn\'t," she said.' '"Isn\'t," she said.'

Strings To input string across multiple lines use the / character hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." Or “”” print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """

Raw Strings To get a raw string, you add an r in front >>>hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C.“ It prints the string exactly ignoring things like newlines …This is a rather long string containing\n\ several lines of text much as you would do in C.

Strings Strings are concatenated with + and repeated with * >>> word = 'Help' + 'A' >>> word 'HelpA' >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>' Placing two literal strings next to each other automatically concatenates them. >>> 'str' 'ing' 'string'

Strings Strings are indexed, starting at 0 >>> word[4] 'He' >>> word[2:4] 'lp' Strings can also be sliced. >>> word[:2] >>> word[2:] 'lpA' Giving negative numbers for the indexes starts the string at the right instead of the left

Lists List do not need to have the same data type throughout >>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234] Like strings, the index starts at 0 and lists can be sliced, concatonated, etc.

Lists It is possible to change a list once its been made. >>> # Replace some items: ... a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] >>> # Remove some: ... a[0:2] = [] >>> a [123, 1234] >>> # Insert some: ... a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234] >>> # Insert (a copy of) itself at the beginning >>> a[:0] = a >>> a [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] >>> # Clear the list: replace all items with an empty list >>> a[:] = [] >>> a []

if Statements >>> x = int(raw_input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More'

for Statements >>> # Measure some strings: ... a = ['cat', 'window', 'defenestrate'] >>> for x in a: ... print x, len(x) ... cat 3 window 6 defenestrate 12

range() Function >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 3) [0, 3, 6, 9] >>> range(-10, -100, -30) [-10, -40, -70]

Other Statements The break statement breaks out of the for or while loop. The continue statement continues onto the next section of the loop. The else clause terminates when the list exhausts or becomes false. The pass funtion is a placeholder. It doesn’t do anything.

Default Argument def() is the default argument for a function. def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):

Keyword Arguments They have the form keyword = value. def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print "-- This parrot wouldn't", action, print "if you put", voltage, "volts through it." print "-- Lovely plumage, the", type print "-- It's", state, "!"

Resource http://docs.python.org/tutorial/index.html