Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
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.
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.
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
1 Introduction to Programming with Python. 2 Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Chapter 5 new The Do…Loop Statement
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Introduction to Computing Using Python Python  Python is an interactive language.  Java or C++: compile, run  Also, a main function or method  Python:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Lists in Python.
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 9 More About Strings.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
>>> # 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.
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.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
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.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
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.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
1 Introduction to Programming with Python: overview.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Input, Output and Variables GCSE Computer Science – Python.
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
Fundamentals of Programming I Overview of Programming
CSc 120 Introduction to Computer Programing II Adapted from slides by
Python: Experiencing IDLE, writing simple programs
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Programming: Module #2 Python, Trinket, and Turtle Graphics Lois Delcambre.
CS 100: Roadmap to Computing
Python: Control Structures
Introduction to Python
Creation, Traversal, Insertion and Removal
CS190/295 Programming in Python for Life Sciences: Lecture 6
CS 100: Roadmap to Computing
“If you can’t write it down in English, you can’t code it.”
String and Lists Dr. José M. Reyes Álamo.
A look at Python Programming Language 2018.
Basic String Operations
The Data Element.
The Data Element.
CS 100: Roadmap to Computing
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language that is ideal for beginners

Python Software Software may be downloaded from: Download the Python 3.x version An IDE (IDLE IDE) is included in the standard implementation

Getting Started….. Basic Arithmetic Operators ◦ +, -, /, *, % ◦ // is used for integer division (Example: 9 // 4 will give you 2) ◦ ** is used for exponentiation ( 2**3 gives 8) Some simple functions: abs(), min(), max()

Boolean Expressions/Operators Boolean values: True, False and (&& in Java), or (|| in Java), not (! In Java) Comparisons: >, >=, <, <=. ==, !=

Variable Names A through Z, lowercase or uppercase, underscore, and digits 0-9 (except for the first character) Variable name cannot start with a digit Examples: account, account63, bank_account, bankAccount, … Starting with Python 3, characters in other languages may be used (Unicode characters) Variables in Python don’t have a type

Strings Enclosed in quotes (single or double may be used) s=‘Hello!’ is an example of a string assignment s==‘Hello!’ evaluates to True s==‘hello!’ evaluates to False s=s + ‘ How are you?’ (String concatenation)

More on strings…. x=‘bonjour’ len(x) gives 7, the length of the string ‘on’ in x evaluates to True (‘on’ is a substring of x) x[0] gives ‘b’ (the character at index 0) x * 2 will give ‘bonjourbonjour’ x[-1] displays ‘r’, the last character x[-2] displays ‘u’, the last but one character

Lists Stores a sequence of objects books = [‘The Jungle Book’, ‘The Idiot’, ‘The Razor’s Edge’] [ ] is an empty list books[0] displays ‘The Jungle Book’ Books[-1] displays ‘The Razor’s Edge’ Lists can contain lists ◦ Example: x = [ 1, [2, 3], 4] ◦ x[1] displays [2, 3]

Some operators and functions + to concatenate lists [1,2] * n will repeat 1,2 n times If x = [1,2,3,4], then: 3 in x will evaluate to True 5 not in x will be True sum(x) will be 10 (sum of items in list) max(x) will be 4, min(x) will be 1

More on lists Lists are mutable, strings are not colors = [‘red’, ‘blue’, ‘green’] colors.append(‘yellow’) appends ‘yellow’ to colors colors.count(‘red’) returns 1, the number of occurrences of ‘red’ in the list colors.remove(‘blue’) removes first occurrence of ‘blue’ colors.reverse() reverses the list color.sort() sorts in ascending order

Math Module Module is similar to the notion of a package import math to use math functions Math functions/constants include sqrt(), ceil(), floor(), cos(), sin(), log(), pi, e

Turtle Graphics #Turtle graphics #This program uses two Turtle objects to draw random lines on the #screen import turtle import random #instantiate the Screen object on which we will be drawing s=turtle.Screen() #first Turtle object turtle1 = turtle.Turtle() turtle1.pencolor('red') turtle1.pendown() turtle2.forward(100)

Useful Built-In Functions print() input() Example: name = input(“What is your name? “) print(“Hello “ + name + “, how are you?”)

If statement Syntax: if : elif : else:

If Example age = int(input(“Enter your age: “)) if age < 10: print(“You are rather young for Python”) else: print(“Conquer the world with Python”)

Iteration For animals = [‘dogs’, ‘cats’, ‘lions’] #iterate through list and display animals for x in animals: print(x) While See example on next slide…

Example of while validNames = [‘Don’, ‘Peter’, ‘Mary’] loginName = input(“Enter login name: “) #stay in loop until user enters the right #user name while loginName not in validNames: loginName = input(“Invalid name! Try again: “) print(“You are in!”)

User-Defined Functions def displayMessage(): print(“Hi there! Have a nice day”) def sum(x, y): return x + y displayMessage() print(sum(3,8))

Strings, substrings s = “goodbye!” What is s[0]? (Ans: ‘g’) s[0:4] would be ‘good’ s[:4] is also ‘good’ s[3:7] would be ‘dbye’ s[4:] would give you ‘bye!’ s[-1] is ? (Ans: ‘!’) s[-3: ] would be ‘ye!’

Some string methods s = ‘bonjour’ s.find(‘on’) returns 1 (first occurrence of ‘on’) s.upper()  ‘BONJOUR’ s.lower()  ? s.replace(‘o’, ‘i’)  ‘binjiur’ s.strip() gets rid of leading/trailing blanks