An Introduction to Python

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Adapted from John Zelle’s Book Slides
Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
Chapter 2 Writing Simple Programs
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python quick start guide
Python Programming Fundamentals
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python Basic Syntax. Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello,
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Input, Output, and Processing
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Whatcha doin'? Aims: To start using Python. To understand loops.
Chapter 6 JavaScript: Introduction to Scripting
Python: Experiencing IDLE, writing simple programs
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
Pamela Moore & Zenia Bahorski
Lesson 1 An Introduction
Variables, Expressions, and IO
Introduction to Scripting
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Chapter 2 - Introduction to C Programming
Please use speaker notes for additional information!
Creation, Traversal, Insertion and Removal
WEB PROGRAMMING JavaScript.
Chapter 2 - Introduction to C Programming
T. Jumana Abu Shmais – AOU - Riyadh
String and Lists Dr. José M. Reyes Álamo.
Chapter 2 - Introduction to C Programming
Python Basics with Jupyter Notebook
Introduction to Computer Science
Chapter 2 - Introduction to C Programming
12th Computer Science – Unit 5
Chopin’s Nocturne.
Class code for pythonroom.com cchsp2cs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

An Introduction to Python By Michael McCarthy

Little Known Fact About Python / Attention Grabber The language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. http://try-python.mired.org/ Little Known Fact About Python / Attention Grabber

In order to use Python you must install the interpreter One way of starting the interpreter is to use: python -c command [arg] This will execute the code within “command”. If you have spaces in your commands you should surround them with single quotes. Functions are used by the command function(object) (for example len(‘bar’) would return the length of the string bar which is 3) len(‘bar’) Getting Started

Uses of Symbols Comments are preceded by # Mathematical operators work as normal (+ means add, - means subtract, etc) An equal sign (=) assigns a value to a variable (i.e., foo = 5 assigns the value 5 to variable foo) Unlike most programming languages that end either lines of code with ; Python’s lines of code are not terminated with symbols Imaginary numbers are followed by j or J (i.e., 5 + 2j or 5 + 2J so (5 + 2j) * 10 = 50 + 20j) When the interpreter is in interactive mode the answer from the last command is stored in the variable _ (if your last command was 5+2 then print _ would output 7) 5+2, 5*2 foo=5, bar=5, foo*bar _/1 5 +2j 2*_ Uses of Symbols

Strings are encapsulated in either single or double quotations Strings are encapsulated in either single or double quotations. For strings spanning over multiple lines each line should be terminated by a \ character to indicate that there is a continuation. This is separate from using \n for a new line Putting r before stating a string converts the text to raw which will evaluate slashes and \n as text Putting u before a string indicates that a unicode string is being created Strings can be combined together with + and repeated with * Strings can be subscripted (slice in some languages) with []. Variable[0:2] would output from the beginning to the 2nd letter. Variable[1:] would output all but the first letter and Variable[:3] outputs the first 3 characters. Lists are similar to other languages – list = [1,2,3] or list = [‘a’,’b’,’c’] Len(list) outputs 3 For example: test = “Foo” test2 = “bar” test = test + test2 would output Foobar test * 3 would output FoobarFoobarFoobar test[1:3] test[:2] test[2:] List = [‘a’,’b’,’c’] Len(list) Strings and Lists

The first line of Loops and statements end in colons (:) and there are no brackets used to denote the contents of your command In Python, pressing enter after the colon brings the user prompt to a new line where you must indent to specify the contents of your loop. Each command should be on a separate line with each line indented. To end the writing of a loop or statement simply press enter/return without any input if q > 5: q = q – 1 print q elif q == 3: print ‘q is 3’ else: print ‘q is other’ For statements are different then normal. For statements iterate over the items of the given sequence in order applying that value to your for variable A traditional for loop would be more like this: for i in range(len(list)): print len[i] num = 0 While num < 12: print num num = num + 2 if q > 5: q = q – 1 print q elif q == 3: print ‘q is 3’ else: print ‘q is other’ Var = [‘monkey’,’donkey’,’blah’] For el in var: print el Len = [‘one’,’two’,’three’] for i in range(len(list)): print len[i] Loops and Statements

Defining Functions Functions are defined as follows: def name(variable): print variable We can rename functions by assigning them to variables: blah = name blah(‘monkey’) outputs monkey Loops and statements can be put inside of functions as normal Def name(variable): print variable Blah = name Blah(‘monkey’) Defining Functions