Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

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.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
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.
Structured programming
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
Python Crash Course by Monica Sweat. Python Perspective is strongly typed, interpreted language is used to define scripts (Don't even need to define a.
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.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 7: Python’s Built-in Types and Basic Statements COMP 144 Programming Language Concepts.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
The Python Programming Language Matt Campbell | Steve Losh.
Basic Elements of C++ Chapter 2.
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 Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
And PyLNP for the RCX By: Eric Cranmer and Nick Blake.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python The tutorial
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 Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
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.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
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:
Chapter 2 Writing Simple Programs
Literals and Variables Chapter 2, Sections
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Basic Elements of C++.
Variables, Expressions, and IO
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Functions CIS 40 – Introduction to Programming in Python
Basic Elements of C++ Chapter 2.
Introduction to Strings
Introduction To Python
An overview of Java, Data types and variables
An Introduction to Python
Wednesday 09/23/13.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Computer Science
Lecture 7: Python’s Built-in Types and Basic Statements
General Computer Science for Engineers CISC 106 Lecture 03
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Sajal Desai

What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!”

Using the Interpreter On Tux, /usr/local/bin/python quit() or crtl+d to quit >>> waiting for next command... continuation lines # denotes everything trailing on the rest of the line is commented

Starting Python python Python 2.6 (#1, Feb , 00:02:06) Type "help", "copyright", "credits" or "license" for more information. >>>

Some Math! The interpreter acts as a simple calculator: –>>> 7/3 2 ‘=‘ assigns the value to a variable –>>> width = 20 –>>> height = 5 * 9 –>>> width * height 900 simultaneously: –x = y= z = 0 **Note** Variables must be defined before used!!! Integer math, double math, float math all work the same as they do in C++ _ is the value of the last variable

Strings! Single or double quotes \ to display escape characters –“doesn\’t’ doesn’t To continue input across a number of lines: – use the \ character: hello = “Hullo? This is a super long string\ hullo hullo.\ all of this is still going into the variable hello. –Alternatively use triple quotes “”” Hello = “”” This is a super super long string but it is being stored in hello. “”” “\n” denotes newline, and still must be embedded in a string

Strings (cont.) Use r to have “raw” input –string = r”Super duper long string\n\n\n” “Super duper long string\n\n\n” Concatenated with + Repeated with * Strings are indexed, 0 is the start For substrings, use slice notation –string[0:6] Super d –string [:3] Supe –string [1:] per duper long string\n\n\n –string[-1] n –string [:-5] Super duper long string\ **NOTE** Strings cannot be changed

Lists Comma separated, do not have to be the same type –a = [hi, bye, 6] Can be indexed, can use slice, concatenated Can change Can use slice to assign, even resize Can nest strings –a = [hi, bye, 6, b] –b = [1,2,3]

if statements >>> x = int (raw_input(“Enter: “)) Enter: 42 >>> int x < 0: x = 0 print “Negative changed to 0” elif x == 0: print x; else: print “X is more than 0”

For statements For statements, unlike C++ Iterates over a sequence of items (think lists), not over an arithmetic progression (i++) Not wise to modify the list being interated, make a copy of the list for x in a[:]: ….stuff….

range() Builds a list of arithmetic progressions –range(10) [0,1…10] Some useful examples: –range(5,10) [5…10] –range (0,10,3) [0,3,6,9] –range (-10, -100, -30) [-10,-40,-70]

Functions def –function definition keyword def fib(n): –contains def name(list of parameters): –everything the function contains must be indented

Functions (cont.) Default arguments –def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): can be called any of these ways: –ask_ok(“Do you really want to quit”) –ask_ok(“Do you really want to quit, 2”) Keyword Arguments –def parrot(voltage, state='a stiff', action='voom', ='Norwegian Blue'): can be called: –parrot(1000) –parrot(action = 'VOOOOOM', voltage = ) –parrot('a thousand', state = 'pushing up the daisies') –parrot('a million', 'bereft of life', 'jump') DocStrings –The first line after defining a function is a comment –Use to write a short description of the function

Conventions 4-space indentations Lines should not exceed 79 characters Use blank lines to separate functions and classes Comment on separate lines Use DocStrings Use spaces around operators, after commas Use CamelCase for classes, lower_case_with_underscores for functions

Source