Download - Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Transcript
Page 1: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python: Part 3Daniel Lucio

Python Crash CourseApril 6, 2016

Page 2: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

Sublime Text 3

https://www.sublimetext.com/

Page 3: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

Setting Up Sublime Text 3 for Python3 Development

Go to tools -- build system -- new build system

{ "cmd": ["/usr/local/bin/python3", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python", "encoding": "utf8", "path": "/usr/local/Frameworks/Python.framework/Versions/3.5/bin/"}

http://www.jasred.com/2014/05/12/setting-up-sublime-text-3-for-python3-development/

Page 4: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

Celcius to Fahrenheit

In this formula, C is the amount of degrees in Celsius, and F is the corresponding temperature measured in Fahrenheit. Our goal now is to write a computer program which can

compute F from when C is known.

F =9

5C + 32

Page 5: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

61 Chapter 4: Reviewing Basic Python

Doing arithmeticStoring information in variables makes it easily accessible. However, in order to perform any useful work with the variable, you usually perform some type of arithmetic operation on it. Python supports the common arithmetic opera-tors you use to perform tasks by hand. They appear in Table 4-2.

Sometimes you need to interact with just one variable. Python supports a number of unary operators, those that work with just one variable, as shown in Table 4-3.

Table 4-2 Python Arithmetic OperatorsOperator Description Example+ Adds two values together 5 + 2 = 7‐ Subtracts the right‐hand operand from left operand 5 – 2 = 3* Multiplies the right‐hand operand by the left operand 5 * 2 = 10/ Divides the left‐hand operand by the right operand 5 / 2 = 2.5% Divides the left‐hand operand by the right operand and

returns the remainder5 % 2 = 1

** Calculates the exponential value of the right operand by the left operand

5 ** 2 = 25

// Performs integer division, in which the left operand is divided by the right operand and only the whole number is returned (also called floor division)

5 // 2 = 2

Table 4-3 Python Unary OperatorsOperator Description Example~ Inverts the bits in a number so that all of the 0 bits

become 1 bits and vice versa.~4 results in a value of –5

‐ Negates the original value so that positive becomes negative and vice versa.

–(–4) results in 4 and –4 results in –4

+ Is provided purely for the sake of completeness. This operator returns the same value that you provide as input.

+4 results in a value of 4

Page 6: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

64 Part I: Getting Started with Python for Data Science

Computers provide order to comparisons by making some operators more significant than others. The ordering of operators is operator precedence. Table 4-7 shows the operator precedence of all the common Python opera-tors, including a few you haven’t seen as part of a discussion yet. When making comparisons, always consider operator precedence because other-wise, the assumptions you make about a comparison outcome will likely be wrong.

Operator Description Exampleor Determines when one of two

operands is true.True or True is True

True or False is True

False or True is True

False or False is Falsenot Negates the truth value of a

single operand. A true value becomes false and a false value becomes true.

not True is False

not False is True

Table 4-6 (continued)

Table 4-7 Python Operator PrecedenceOperator Description() You use parentheses to group expressions and to override

the default precedence so that you can force an operation of lower precedence (such as addition) to take precedence over an operation of higher precedence (such as multiplication).

** Exponentiation raises the value of the left operand to the power of the right operand.

~ + ‐ Unary operators interact with a single variable or expression.* / % // Multiply, divide, modulo, and floor division.+ ‐ Addition and subtraction.>> << Right and left bitwise shift.& Bitwise AND.^ | Bitwise exclusive OR and standard OR.<= < > >= Comparison operators.

Page 7: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v1What is the equivalent of 21C?

print((9/5)*21+32)

Page 8: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v2 (Using Variables)What is the equivalent of 21C?

c=21f=(9/5)*c+32print(f)

Page 9: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v3 (Using Functions)What is the equivalent of 21C?

defc2f(c):return((9/5)*c+32)

print(c2f(21))

Page 10: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v4 (Using Functions)What is the equivalent of 21C?

defc2f(c):return((9/5)*c+32)

print(c2f(21))

Page 11: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F (Looping)How to create a conversion table?

-20-4.0-155.0-1014.0-523.0032.0541.01050.01559.02068.02577.03086.03595.040104.0

C F

Page 12: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

63 Chapter 4: Reviewing Basic Python

Sometimes a relational operator can’t tell the whole story of the comparison of two values. For example, you might need to check a condition in which two

separate comparisons are needed, such as MyAge > 40 and MyHeight < 74. The need to add conditions to the comparison requires a logical operator of the sort shown in Table 4-6.

Table 4-5 Python Relational OperatorsOperator Description Example== Determines whether two values are equal. Notice

that the relational operator uses two equals signs. A mistake many developers make is using just one equals sign, which results in one value being assigned to another.

1 == 2 is False

!= Determines whether two values are not equal. Some older versions of Python allowed you to use the <> operator in place of the != operator. Using the <> operator results in an error in current versions of Python.

1 != 2 is True

> Verifies that the left operand value is greater than the right operand value.

1 > 2 is False

< Verifies that the left operand value is less than the right operand value.

1 < 2 is True

>= Verifies that the left operand value is greater than or equal to the right operand value.

1 >= 2 is False

<= Verifies that the left operand value is less than or equal to the right operand value.

1 <= 2 is True

Table 4-6 Python Logical OperatorsOperator Description Exampleand Determines whether both operands

are true.True and True is True

True and False is False

False and True is False

False and False is False(continued)

Page 13: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v5 (while loop)How to create a conversion table for -20 to 40C?

c=-20whilec<=40: print(c,(9/5)*c+32) c=c+5

c=-20whilec<=40: print(c,(9/5)*c+32) c+=5

See the difference?

Page 14: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

60 Part I: Getting Started with Python for Data Science

Performing variable assignmentsWhen working with applications, you store information in variables. A vari-able is a kind of storage box. Whenever you want to work with the informa-tion, you access it using the variable. If you have new information you want to store, you put it in a variable. Changing information means accessing the variable first and then storing the new value in the variable. Just as you store things in boxes in the real world, so you store things in variables (a kind of storage box) when working with applications. To store data in a variable, you assign the data to it using any of a number of assignment operators (special symbols that tell how to store the data). Table 4-1 shows the assignment operators that Python supports.

Table 4-1 Python Assignment OperatorsOperator Description Example= Assigns the value found in the right operand

to the left operandMyVar = 2 results in MyVar containing 2

+= Adds the value found in the right operand to the value found in the left operand and places the result in the left operand

MyVar += 2 results in MyVar containing 7

‐= Subtracts the value found in the right oper-and from the value found in the left operand and places the result in the left operand

MyVar ‐= 2 results in MyVar containing 3

*= Multiplies the value found in the right oper-and by the value found in the left operand and places the result in the left operand

MyVar *= 2 results in MyVar containing 10

/= Divides the value found in the left operand by the value found in the right operand and places the result in the left operand

MyVar /= 2 results in MyVar containing 2.5

%= Divides the value found in the left operand by the value found in the right operand and places the remainder in the left operand

MyVar %= 2 results in MyVar containing 1

**= Determines the exponential value found in the left operand when raised to the power of the value found in the right operand and places the result in the left operand

MyVar ** 2 results in MyVar containing 25

//= Divides the value found in the left operand by the value found in the right operand and places the integer (whole number) result in the left operand

MyVar //= 2 results in MyVar containing 2

Page 15: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v6 (while loop)How to create a conversion table for -20 to 40C?

c=-20whilec<=40: print(c,(9/5)*c+32) c=c+5

c=-20whilec<=40: print(c,(9/5)*c+32) c+=5

See the difference?

Page 16: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v7 (for loop)How to create a conversion table for -20 to 40C?

degrees=[-20,-15,-10,-5,0,5,10,15,20,25,30,35,40]forcindegrees: print(c,(9/5)*c+32)

forcinrange(-20,45,5): print(c,(9/5)*c+32)

Page 17: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v9 (pretty print)How to create a conversion table for -20 to 40C?

forcinrange(-20,45,5): print(c,(9/5)*c+32)

forcinrange(-20,45,5):print('%5d%5.1f'%(c,(9/5)*c+32))

vs

forcinrange(-20,45,5): print('{0:5d}{1:5.1f}'.format(c,(9/5)*c+32))

vs

Page 18: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v10 (Nested Lists)How to create a conversion table for -20 to 40C?

Cdegrees=range(-20,45,5)Fdegrees=[(9.0/5)*C+32forCinCdegrees]table=[]forC,Finzip(Cdegrees,Fdegrees):table.append([C,F])print(table)

Page 19: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

C2F, v11 (Nested Lists)How to create a conversion table for -20 to 40C?

importpprintCdegrees=range(-20,45,5)Fdegrees=[(9.0/5)*C+32forCinCdegrees]table=[]forC,Finzip(Cdegrees,Fdegrees):table.append([C,F])pprint.pprint(table)

Page 20: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

Questions?

Page 21: Python: Part 3 - National Institute for · PDF filePython: Part 3 Daniel Lucio Python Crash Course April 6, 2016. Python Crash Course Sublime Text 3 Python Crash Course Setting Up

Python Crash Course

More informationPython Crash Course: A Hands-On, Project-

Based Introduction to ProgrammingEric Matthes

A Primer on Scientific Programming with Python

Hans Petter Langtangen

Python for Data Science For Dummies John Paul Mueller , Luca Massaron