Python language: Basics The FOSSEE Group Department of Aerospace - - PowerPoint PPT Presentation

python language basics
SMART_READER_LITE
LIVE PREVIEW

Python language: Basics The FOSSEE Group Department of Aerospace - - PowerPoint PPT Presentation

Python language: Basics The FOSSEE Group Department of Aerospace Engineering IIT Bombay Mumbai, India FOSSEE Team (FOSSEE IITB) Basic Python 1 / 45 Outline Data types 1 Numbers Booleans Strings Operators 2 Simple IO 3 FOSSEE


slide-1
SLIDE 1

Python language: Basics

The FOSSEE Group

Department of Aerospace Engineering IIT Bombay

Mumbai, India

FOSSEE Team (FOSSEE – IITB) Basic Python 1 / 45

slide-2
SLIDE 2

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 2 / 45

slide-3
SLIDE 3

Data types

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 3 / 45

slide-4
SLIDE 4

Data types

Primitive Data types

Numbers: float, int, complex Strings Booleans

FOSSEE Team (FOSSEE – IITB) Basic Python 4 / 45

slide-5
SLIDE 5

Data types Numbers

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 5 / 45

slide-6
SLIDE 6

Data types Numbers

Numbers

int

whole number, no matter what the size!

In []: a = 13 In []: b = 99999999999999999999 float In []: p = 3.141592 complex In []: c = 3+4j In []: c = complex(3, 4)

FOSSEE Team (FOSSEE – IITB) Basic Python 6 / 45

slide-7
SLIDE 7

Data types Booleans

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 7 / 45

slide-8
SLIDE 8

Data types Booleans

Booleans

In []: t = True In []: F = not t In []: F or t Out[]: True In []: F and t Out[]: False

FOSSEE Team (FOSSEE – IITB) Basic Python 8 / 45

slide-9
SLIDE 9

Data types Booleans

( ) for precedence

In []: a = False In []: b = True In []: c = True In []: (a and b) or c Out[]: True In []: a and (b or c) Out[]: False

FOSSEE Team (FOSSEE – IITB) Basic Python 9 / 45

slide-10
SLIDE 10

Data types Strings

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 10 / 45

slide-11
SLIDE 11

Data types Strings

Strings

Anything within “quotes” is a string!

’ This is a string ’ " This too! " """ This one too! """ ’’’ And one more! ’’’

FOSSEE Team (FOSSEE – IITB) Basic Python 11 / 45

slide-12
SLIDE 12

Data types Strings

Strings

Why so many?

’ "Do or do not. No try." said Yoda.’ " ’ is a mighty lonely quote."

The triple quoted ones can span multiple lines!

""" The quick brown fox jumped over the lazy dingbat. """

FOSSEE Team (FOSSEE – IITB) Basic Python 12 / 45

slide-13
SLIDE 13

Data types Strings

Strings

In []: w = "hello" In []: print(w[0], w[1], w[-1]) In []: len(w) Out[]: 5

FOSSEE Team (FOSSEE – IITB) Basic Python 13 / 45

slide-14
SLIDE 14

Data types Strings

Strings . . .

Strings are immutable

In []: w[0] = ’H’

  • TypeError

Traceback (most recent call last) <ipython console> in <module>() TypeError: ’str’ object does not support item assignment

FOSSEE Team (FOSSEE – IITB) Basic Python 14 / 45

slide-15
SLIDE 15

Data types Strings

Strings . . .

Strings are immutable

In []: w[0] = ’H’

  • TypeError

Traceback (most recent call last) <ipython console> in <module>() TypeError: ’str’ object does not support item assignment

FOSSEE Team (FOSSEE – IITB) Basic Python 14 / 45

slide-16
SLIDE 16

Data types Strings

Finding the type

In []: a = 1.0 In []: type(a) Out[]: float In []: type(1) Out[]: int In []: type(1+1j) Out[]: complex In []: type(’hello’) Out[]: str

FOSSEE Team (FOSSEE – IITB) Basic Python 15 / 45

slide-17
SLIDE 17

Operators

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 16 / 45

slide-18
SLIDE 18

Operators

Arithmetic operators

In []: 1786 % 12 Out[]: 10 In []: 45 % 2 Out[]: 1 In []: 864675 % 10 Out[]: 5 In []: 3124 * 126789 Out[]: 396088836 In []: big = 1234567891234567890 ** 3 In []: verybig = big * big * big * big

FOSSEE Team (FOSSEE – IITB) Basic Python 17 / 45

slide-19
SLIDE 19

Operators

Arithmetic operators

In []: 17 / 2 Out[]: 8.5 # 8 on Python 2.x In []: 17 / 2.0 Out[]: 8.5 In []: 17.0 / 2 Out[]: 8.5 In []: 17.0 / 8.5 Out[]: 2.0

FOSSEE Team (FOSSEE – IITB) Basic Python 18 / 45

slide-20
SLIDE 20

Operators

Arithmetic operators: floor division

In []: 17 // 2 Out[]: 8 In []: 17 // 2.0 Out[]: 8.0 In []: 17.0 // 2.0 Out[]: 8.0 In []: 17.0 // 8.6 Out[]: 1.0

FOSSEE Team (FOSSEE – IITB) Basic Python 19 / 45

slide-21
SLIDE 21

Operators

Arithmetic operators

In []: c = 3+4j In []: abs(c) Out[]: 5.0 In []: c.imag Out[]: 4.0 In []: c.real Out[]: 3.0 In []: c.conjugate() (3-4j)

FOSSEE Team (FOSSEE – IITB) Basic Python 20 / 45

slide-22
SLIDE 22

Operators

Arithmetic operators

In []: a = 7546 In []: a += 1 In []: a Out[]: 7547 In []: a -= 5 In []: a In []: a *= 2 In []: a /= 5

FOSSEE Team (FOSSEE – IITB) Basic Python 21 / 45

slide-23
SLIDE 23

Operators

String operations

In []: s = ’Hello’ In []: p = ’World’ In []: s + p Out[]: ’HelloWorld’ In []: s * 4 Out[]: ’HelloHelloHelloHello’

FOSSEE Team (FOSSEE – IITB) Basic Python 22 / 45

slide-24
SLIDE 24

Operators

String operations . . .

In []: s * s

  • TypeError

Traceback (most recent call last) <ipython console> in <module>() TypeError: can‘t multiply sequence by non-int of type ‘str‘

FOSSEE Team (FOSSEE – IITB) Basic Python 23 / 45

slide-25
SLIDE 25

Operators

String operations . . .

In []: s * s

  • TypeError

Traceback (most recent call last) <ipython console> in <module>() TypeError: can‘t multiply sequence by non-int of type ‘str‘

FOSSEE Team (FOSSEE – IITB) Basic Python 23 / 45

slide-26
SLIDE 26

Operators

String methods

In []: a = ’Hello World’ In []: a.startswith(’Hell’) Out[]: True In []: a.endswith(’ld’) Out[]: True In []: a.upper() Out[]: ’HELLO WORLD’ In []: a.lower() Out[]: ’hello world’

FOSSEE Team (FOSSEE – IITB) Basic Python 24 / 45

slide-27
SLIDE 27

Operators

String methods

In []: a = ’ Hello World ’ In []: b = a.strip() In []: b Out[]: ’Hello World’ In []: b.index(’ll’) Out[]: 2 In []: b.replace(’Hello’, ’Goodbye’) Out[]: ’Goodbye World’

FOSSEE Team (FOSSEE – IITB) Basic Python 25 / 45

slide-28
SLIDE 28

Operators

Strings: split & join

In []: chars = ’a b c’ In []: chars.split() Out[]: [’a’, ’b’, ’c’] In []: ’ ’.join([’a’, ’b’, ’c’]) Out[]: ’a b c’ In []: alpha = ’, ’.join([’a’, ’b’, ’c’]) In []: alpha Out[]: ’a, b, c’ In []: alpha.split(’, ’) Out[]: [’a’, ’b’, ’c’]

FOSSEE Team (FOSSEE – IITB) Basic Python 26 / 45

slide-29
SLIDE 29

Operators

String formatting

In []: x, y = 1, 1.234 In []: ’x is %s, y is %s’ %(x, y) Out[]: ’x is 1, y is 1.234’

%d, %f etc. available

http://docs.python.org/library/ stdtypes.html

FOSSEE Team (FOSSEE – IITB) Basic Python 27 / 45

slide-30
SLIDE 30

Operators

Relational and logical operators

In []: p, z, n = 1, 0, -1 In []: p == n Out[]: False In []: p >= n Out[]: True In []: n < z < p Out[]: True In []: p + n != z Out[]: False

FOSSEE Team (FOSSEE – IITB) Basic Python 28 / 45

slide-31
SLIDE 31

Operators

The assert statement

You will see it in tests and your exam!

In []: assert p != n In []: assert p == n

  • AssertionError

Traceback (most recent call last)

  • ---> 1 assert p == n

AssertionError:

No error if condition is True Raises error if False

FOSSEE Team (FOSSEE – IITB) Basic Python 29 / 45

slide-32
SLIDE 32

Operators

assert examples

In []: assert p == n, "Oops condition failed"

  • AssertionError

Traceback (most recent call last)

  • ---> 1 assert p == n

AssertionError: Oops condition failed

Can supply an optional message

FOSSEE Team (FOSSEE – IITB) Basic Python 30 / 45

slide-33
SLIDE 33

Operators

String containership

In []: fruits = ’apple, banana, pear’ In []: ’apple’ in fruits Out[]: True In []: ’potato’ in fruits Out[]: False

Use tab complete to list other string methods Use ? to find more information

FOSSEE Team (FOSSEE – IITB) Basic Python 31 / 45

slide-34
SLIDE 34

Operators

Built-ins

In []: int(17 / 2.0) Out[]: 8 In []: float(17 // 2) Out[]: 8.0 In []: str(17 / 2.0) Out[]: ’8.5’ In []: round( 7.5 ) Out[]: 8.0

FOSSEE Team (FOSSEE – IITB) Basic Python 32 / 45

slide-35
SLIDE 35

Operators

Odds and ends

Case sensitive Dynamically typed ⇒ need not specify a type

In []: a = 1 In []: a = 1.1 In []: a = "Now I am a string!"

Comments:

In []: a = 1 # In-line comments In []: # A comment line. In []: a = "# Not a comment!"

FOSSEE Team (FOSSEE – IITB) Basic Python 33 / 45

slide-36
SLIDE 36

Operators

Exercise 1

Given a 2 digit integer x, find the digits of the number. For example, let us say x = 38 Find a way to get a = 3 and b = 8 using x?

FOSSEE Team (FOSSEE – IITB) Basic Python 34 / 45

slide-37
SLIDE 37

Operators

Possible Solution

In []: a = x//10 In []: b = x%10 In []: a*10 + b == x

FOSSEE Team (FOSSEE – IITB) Basic Python 35 / 45

slide-38
SLIDE 38

Operators

Another Solution

In []: sx = str(x) In []: a = int(sx[0]) In []: b = int(sx[1]) In []: a*10 + b == x

FOSSEE Team (FOSSEE – IITB) Basic Python 36 / 45

slide-39
SLIDE 39

Operators

Exercise 2

Given an arbitrary integer, count the number of digits it has.

FOSSEE Team (FOSSEE – IITB) Basic Python 37 / 45

slide-40
SLIDE 40

Operators

Possible solution

In []: x = 12345678 In []: len(str(x))

Sneaky solution!

FOSSEE Team (FOSSEE – IITB) Basic Python 38 / 45

slide-41
SLIDE 41

Simple IO

Outline

1

Data types Numbers Booleans Strings

2

Operators

3

Simple IO

FOSSEE Team (FOSSEE – IITB) Basic Python 39 / 45

slide-42
SLIDE 42

Simple IO

Simple IO: Console Input

input() waits for user input. In []: a = input() 5 In []: a Out[]: ’5’ # Python 3.x! In []: a = input(’Enter a value: ’) Enter a value: 5

Prompt string is optional.

input produces strings (Python 3.x) int() converts string to int.

FOSSEE Team (FOSSEE – IITB) Basic Python 40 / 45

slide-43
SLIDE 43

Simple IO

Simple IO: Python 2.x vs 3.x

print is familiar to us

Changed from Python 2.x to 3.x We use the Python 3 convention here If on Python 2.x do this:

In []: from __future__ import print_function

Safe to use in Python 3.x also

FOSSEE Team (FOSSEE – IITB) Basic Python 41 / 45

slide-44
SLIDE 44

Simple IO

Simple IO: Console output

Put the following code snippet in a file hello1.py

from __future__ import print_function print("Hello", "World") print("Goodbye", "World")

Now run it like so:

In []: %run hello1.py Hello World Goodbye World

FOSSEE Team (FOSSEE – IITB) Basic Python 42 / 45

slide-45
SLIDE 45

Simple IO

Simple IO: Console output . . .

Put the following code snippet in a file hello2.py

from __future__ import print_function print("Hello", end=’ ’) print("World")

Now run it like so:

In []: %run hello2.py Hello World

Mini Exercise Read docs for print Remember: use print?

FOSSEE Team (FOSSEE – IITB) Basic Python 43 / 45

slide-46
SLIDE 46

Simple IO

Simple IO: Console output . . .

Put the following code snippet in a file hello2.py

from __future__ import print_function print("Hello", end=’ ’) print("World")

Now run it like so:

In []: %run hello2.py Hello World

Mini Exercise Read docs for print Remember: use print?

FOSSEE Team (FOSSEE – IITB) Basic Python 43 / 45

slide-47
SLIDE 47

Simple IO

What did we learn?

Data types: int, float, complex, boolean, string Use type builtin function to find the type Operators: +, -, *, /, %, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c Simple IO: input and print

FOSSEE Team (FOSSEE – IITB) Basic Python 44 / 45

slide-48
SLIDE 48

Simple IO

Homework

Explore the various string methods Read the documentation for the string methods Explore the different builtins seen so far also

FOSSEE Team (FOSSEE – IITB) Basic Python 45 / 45