Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Basics.

Similar presentations


Presentation on theme: "Python Basics."— Presentation transcript:

1 Python Basics

2 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

3 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

4 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

5 Logical operators Three operators and or not

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

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

8 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')

9 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

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

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

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

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

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

15 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))

16 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

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

18 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

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

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

21 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

22 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

23 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

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


Download ppt "Python Basics."

Similar presentations


Ads by Google