Python Basics.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 4 - Control Statements
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Conditional Statements Introduction to Computing Science and Programming I.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 4: Conditionals and Recursion
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Geography 465 Assignments, Conditionals, and Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Programming for Linguists An Introduction to Python 17/11/2011.
4. Python - Basic Operators
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming for Linguists An Introduction to Python 24/11/2011.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Python Conditionals chapter 5
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Input, Output and Variables GCSE Computer Science – Python.
String and Lists Dr. José M. Reyes Álamo.
G. Pullaiah College of Engineering and Technology
Python unit_4 review Tue/Wed, Dec 1-2
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
PH2150 Scientific Computing Skills
Line Continuation, Output Formatting, and Decision Structures
C# Basics These slides are designed for Game Design Class
Python - Strings.
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
SELECTIONS STATEMENTS
Python Basics with Jupyter Notebook
COMPUTING.
Presentation transcript:

Python Basics

Today We attempt to use a notebook. It uses ipython to run live code in a browser The notebook will be put online, try having a play with it

Basic math There are int and float types (but not doubles) variables are assigned on the fly Numbers are treated as you would expect (mostly) multiplication, division, exponents etc. are available

Boolean expressions x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to x == y # x is y

Logical operators Three operators and or not

Conditional execution if x > 0: print ('x is positive') if x < 0: pass # need to handle negative values

Alternative execution if x % 2 == 0: print ('x is even') else: print ('x is odd')

Chained conditionals if x < y: print ('x is less than y') elif x > y: print ('x is greater than y') else: print ('x and y are equal')

Chained conditionals There is no switch case in python! if choice == 1: print (‘opt 1’) elif choice == 2: print (‘opt 2’) elif choice == '3': print (‘opt 3’) else: # is optional but must be at the end

Nested conditionals if x ==y: print ('x and y are equal') else: print ('x is less than y') print ('x is greater than y')

Recursion def countdown(n): if n <= 0: print ('Blastoff!') else: print (n) countdown(n-1)

Infinite recursion def recurse(): recurse() File "<stdin>", line 2, in recurse RuntimeError: Maximum recursion depth exceeded

Keyboard input name1= input() name2= input('What is your name?') number = int(input('Enter an integer’)) What happens if user enters a string?

Return values def area(radius): temp = math.pi * radius**2 return temp return math.pi * radius**2

Composition def circle_area(xc, yc, xp, yp): radius = distance(xc, yc, xp, yp) result = area(radius) return result return area(distance(xc, yc, xp, yp))

Updating variables In Java and C we have: x++ and ++x x-- and --x In Python we have: x = x + 1 x = x – 1 x += 1 x -= 1

The while statement def countdown(n): while (n > 0): print (n) print ("Blastoff!")

Working with strings Strings are treated like lists/arrays >>> fruit = 'banana' >>> letter = fruit[1] What does letter = ?? The last letter can be retrieved with fruit[-1] Strings have a ‘len’ function to get their length >>> len(fruit) = 6

Working with strings You can loop over a string: >>> for letter in fruit: Or >>> index = 0 >>> While index < len(fruit):

String slices A segment of a string is called a slice: >>> s = 'Monty Python' >>> print (s[0:5]) Monty >>> print (s[6:12]) Python

String methods A method is similar to a function Method upper takes a string and returns uppercase version of it: >>> word = 'banana' >>> new_word = word.upper() >>> print (new_word) BANANA

The in operator The word in is a boolean operator that returns True if the first string is a substring of the second >>> 'a' in 'banana' True >>> 'seed' in 'banana' False

The in operator def in_both(word1, word2): for letter in word1: if letter in word2: print (letter) >>> in_both('apples', 'oranges') a e s

String comparison if word == 'banana': print ('All right, bananas') print (word + 'comes before banana') if word > 'banana': print (word + 'comes after banana')