Python The tutorial

Slides:



Advertisements
Similar presentations
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Advertisements

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Python November 14, Unit 7. Python Hello world, in class.
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.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Python Programming Fundamentals
Programming for Linguists An Introduction to Python 24/11/2011.
Munster Programming Training
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Walk through previous lecture. TSP Questions: How many tours are there? How do you solve it? How fast can you solve it?
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
H3D API Training  Part 3.1: Python – Quick overview.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
>>> # 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.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
Python Jim Eng Vote on class policy In lecture we discussed several ways in which you might show credits for content in the web pages.
1 E0001 Computers in Engineering Built in Functions.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
# Author: David Foltz # # Notice: Orginal Code Idea for program taken from Python v3.2.2 Documentation def Fibonacci(): """Print a Fibonacci series with.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Variables, Expressions, and Statements
Maths in Python [ slide 5 ] 1.Copy the table 2.Race a friend with a calculator to see whether Python is faster than a calculator: a) 5 * 6.5 = b)7 / 3.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
 Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python.
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.
Python Let’s get started!.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
GCSE Computing: Programming GCSE Programming Remembering Python.
Variables, Types, Expressions Intro2CS – week 1b 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
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"
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
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()
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Chapter 2 Writing Simple Programs
CMSC201 Computer Science I for Majors Lecture 02 – Intro to Python
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Topic: Python’s building blocks -> Statements
Data Types and Conversions, Input from the Keyboard
Presented By S.Yamuna AP/IT
Iterations Programming Condition Controlled Loops (WHILE Loop)
CSCE 590 Web Scraping Topics Readings: Overview – Web scraping
Introduction To Python
Teaching London Computing
مبانی برنامه‌سازی Fundamentals of Programming
CSCE 590 Web Scraping Topics Readings: Overview – Web scraping
Introduction to Programming Using Python PART 2
Python Basics with Jupyter Notebook
Introduction to Computer Science
Errors.
COMPUTING.
Control 9 / 25 / 19.
Presentation transcript:

Python The tutorial

Python as a Calculator >>> >>> # This is a comment >>> 2+2 # and a comment on the same line as code 4 >>> (50-5*6)/4 5 >>> # Integer division returns the floor:... 7/3 2 >>> 7/-3 -3

Assignments >>> width = 20 >>> height = 5*9 >>> width * height 900

Multiple assignments >>> x = y = z = 0 # Zero x, y and z >>> x 0 >>> y 0 >>> z 0

Strings >>> 'spam eggs' 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," he said.' ‘"Yes," he said.' >>> "\"Yes,\" he said.“ ‘ "Yes," he said.' >>> '"Isn\'t," she said.' ‘"Isn\'t," she said.'

Multiple lines 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." print hello This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.

Concatenation >>> word = 'Help' + 'A' >>> word 'HelpA' >>> ' ' ' '

String Manipulation >>> word[4] 'A' >>> word[0:2] 'He' >>> word[2:4] 'lp' >>> word[:2] # The first two characters 'He' >>> word[2:] # Everything except the first two characters 'lpA'

If-statements >>> x = int(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'... More

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

The range() function >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 3) [0, 3, 6, 9] >>> range(-10, -100, -30) [-10, -40, -70]

Functions def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... a, b = 0, 1... while a < n:... print a,... a, b = b, a+b

Calling a function... fib(2000)

More on functions >>> def fib2(n): # return Fibonacci series up to n... """Return a list containing the Fibonacci series up to n."""... result = []... a, b = 0, 1... while a < n:... result.append(a) # see below... a, b = b, a+b... return result... >>> f100 = fib2(100) # call it >>> f100 # write the result [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]