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.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Jay Summet CS 1 with Robots IPRE Python Review 1.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
CPS120: Introduction to Computer Science Lecture 8.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
Fundamentals of Python: First Programs
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Variables, Expressions and Statements
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
ICT Introduction to Programming Chapter 4 – Control Structures I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Fundamentals of Programming I Overview of Programming
CSC 222: Object-Oriented Programming Fall 2017
Python Review 1.
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Multiple variables can be created in one declaration
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
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic operations, decisions and looping
Introduction to Python
Chapter 2: Basic Elements of Java
An Introduction to Python
PHP.
Topics Designing a Program Input, Processing, and Output
Lecture 2 Python Programming & Data Types
Introduction to Programming with Python
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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 function proper.) is used to define functions can be used in object-oriented style is an up and coming real language in industry

Integrated Development Environment: IDLE IDLE is provided with standard python installation available for virtually any platform provides an editing window provides an interactive interpreter window

Starting Simply can use IDLE / python as a calculator >>> >>> 4 ** 2 16 >>> 6 / 2 3 >>> 1 / 2 0  gotcha that begs an introduction to types

Common Types sampling of types – int signed integer, 32 bits, ±2,147,483,647 – longarbitrarily long ends with L – float64 bit double precision, like 1.23 or 7.8e- 28 – strdelimit in ' or " – bool Booleans True and False type(x) yields the type of the data stored currently in variable x

str Type str is the type for strings in python they can be single quote or double quote delimited word = "can't" multiline strings can be triple quoted (that's 3 single quotes or 3 double quotes in a row) poem = """Roses are red, violets are blue."""

Comments (relates to strings) comments in python are single line and use the # symbol # This program written by George Burdell multiline comments can be faked by using the triple quoted string trick """This program written by Monica Sweat July 8, 2008"""

Math Operations ()parens for grouping +-unary plus, minus **exponentiation */%mult-style ops +-add, subtract

Simple Demo of Variables >>> age = 18 >>> print age 18 >>> age = age + 1 >>> print age 19

Python Variables Python is case-sensitive – variable names – function names – everything Camel case is a common style in python, java, etc. firstName = 'George' lastName = 'Burdell' print firstName, lastName word = "don't"

Python Variables [a-zA-Z][a-zA-Z0-9 _]* start with letter followed by letters and digits any length case matters do not have to be declared! python is strongly typed, but the "type" is bound to the data and not the variable

Script Example Complete script using variables Assignment statement uses = hours = 35 rate = pay = hours * rate print pay

Printing Results Increasingly better ways to print pay hours = 35 rate = pay = hours * rate print pay or print "Your pay is", pay or print "Your pay is $" + str(pay) or print "Your pay is $%.2f" % pay

Easy User Input Use input for console input of a numeric value hours = input("How many hours? ") rate = input("Pay rate? ") pay = hours * rate print "Your pay is $%.2f" % pay

Getting Input from the User numeric input age = input("Age? ") string input name = raw_input("Name? ")

Type Conversion x = int(3.9) y = float(5) print "Answer: " + str(2 * 3) (Useful if concatenating with a string) age = int(raw_input("Age? ")) (Useful if using raw_input to get all input from the user.) average = float(sum)/float(total) (Forces float division)

Conditionals if if/else if/elif/elif/elif/…/else all branches use colon : all bodies are indented Python uses indentation to denote blocks.

Building Conditions relational operators as expected, =, ==, !=, <> (assignment uses single equals sign) logical operators are lowercase not, and, or ( ! can be used for not) parentheses not necessary unless overriding precedence

Conditionals age = input("Age? ") if age <= 12: ticketPrice = 5 elif age >= 65: ticketPrice = 7.5 else: ticketPrice = 9 print ticketPrice

Defining a Function def rectangleArea(length, width): area = length * width return area

Using Modules import math def circleArea(radius): area = math.pi * radius ** 2 return area math, random, re, os, …

Importing Modules import math print math.pi x = math.sqrt(9) vs. from math import pi, sqrt print pi x = sqrt(9)

Python Sequences (arrays) python's answer to the array is dynamic behaves like a linked listed with indexing names = ["Fred", "Wilma"] names[0] = "Barney" print names[0]

Python Sequences (arrays) common operations (there are many) names = names + ["Dino"] or names.append("Bambam")

for Loop for-each style iterates through a sequence syntax: for item in sequence: block-of-code for flavor in ["chocolate", "vanilla"]: print "I like", flavor

for Loop def average(array): sum = 0 count = 0 for num in array: sum = sum + num count = count + 1 return float(sum)/count

Counting-style for Loop for loop with counting style using range: for num in range(1, 5): print num Output:

Processing Array without/with range for-each style to iterate through a sequence: for flavor in ["chocolate", "vanilla"]: print "I like", flavor vs. index-driven style processing a sequence: flavors = ["chocolate", "vanilla", "mint"] for index in range(len(flavors)): print "I like", flavors[index]

range Function Details most commonly used to generate indices for iterating a sequence may have 1, 2, or 3 parameters stopping value is exclusive >>> range(5) (starts at 0 if one parameter) [0, 1, 2, 3, 4] >>> range(1995, 2000) (stopping value exclusive) [1995, 1996, 1997, 1998, 1999] >>> range(0, 55, 5) (3 rd parameter is step value) [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

range Function to Generate Data def summation(n): total = 0 for num in range(n + 1): total = total + num return total

while Loop number = 1 while number <= 5: print number number += 1

myro – Major Bonuses myro defined as a proper module – programmer imports it as they would any module – natural way to incorporate added functionality works with standard python – all of standard python is available to the user – other modules: math, random, os, etc are still available – materials for python still apply