Lecture 1 1 What is Python n Python

  • Slides: 25
Download presentation
Lecture 1 1

Lecture 1 1

What is Python? n Python is a popular programming language. It was created in

What is Python? n Python is a popular programming language. It was created in 1991 by Guido van Rossum. n It is used for: n web development (server-side). n software development, GUI n Mathematics. n system scripting. n Networks 2

What can Python do? n Python can be used on a server to create

What can Python do? n Python can be used on a server to create web applications. n Python can be used alongside software to create workflows. n Python can connect to database systems. It can also read and modify files. n Python can be used to handle big data and perform complex mathematics. n Python can be used for rapid prototyping, or for production-ready software development. 3

Why Python? n Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

Why Python? n Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). n Python has a simple syntax similar to the English language. n Python has syntax that allows developers to write programs with fewer lines than some other programming languages. n Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. n Python can be treated in a procedural way, an object-orientated way or a functional way. 4

Try It Out! n Download Python from www. python. org n Any version will

Try It Out! n Download Python from www. python. org n Any version will do for this class n n By and large they are all mutually compatible n Recommended version: 2. 7 or 3. 7 Use IDLE if you can 5

Programming basics n n code or source code: The sequence of instructions in a

Programming basics n n code or source code: The sequence of instructions in a program. syntax: The set of legal structures and commands that can be used in a particular programming language. n output: The messages printed to the user by a program. n console: The text box onto which output is printed. n Some source code editors pop up the console as an external window, and others contain their own console window. 6

Compiling and interpreting n Many languages require you to compile (translate) your program into

Compiling and interpreting n Many languages require you to compile (translate) your program into a form that the machine understands. compile source code Hello. java n byte code Hello. class execute output Python is instead directly interpreted into machine instructions. interpret source code Hello. py output 7

The Python Command Line n n § to test a short amount of code

The Python Command Line n n § to test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself. Type the following on the Windows, Mac or Linux command line: C: UsersYour Name>python From there you can write any python, including our hello world example from earlier in the tutorial: 8

Python Variables n variable: A named piece of memory that can store a value.

Python Variables n variable: A named piece of memory that can store a value. n Usage: n n Compute an expression's result, store that result into a variable, and use that variable later in the program. assignment statement: Stores a value into a variable. n Syntax: name = value n Examples: x = 5 gpa = 3. 14 x 5 gpa 3. 14 n n A variable that has been given a value can be used in expressions. x + 4 is 9 Exercise: Evaluate the quadratic equation for a given a, b, and c. 9

Python Variables n n n Unlike other programming languages, Python has no command for

Python Variables n n n Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Variables do not need to be declared with any particular type and can even change type after they have been set. 10

Python Variables n A variable can have a short name (like x and y)

Python Variables n A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character n A variable name cannot start with a number n A variable name can only contain alpha-numeric characters and underscores (A-z, 0 -9, and _ ) n Variable names are case-sensitive (age, Age and AGE are three different variables) n Remember that variables are case-sensitive 11

Python Variables Output Variables n The Python print statement is often used to output

Python Variables Output Variables n The Python print statement is often used to output variables. n To combine both text and a variable, Python uses the + character: n You can also use the + character to add a variable to another variable: 12

Python Variables n n For numbers, the + character works as a mathematical operator:

Python Variables n n For numbers, the + character works as a mathematical operator: If you try to combine a string and a number, Python will give you an error 13

Expressions n expression: A data value or set of operations to compute a value.

Expressions n expression: A data value or set of operations to compute a value. Examples: n Arithmetic operators we will use: n n 1 + 4 * 3 42 + - * / % ** addition, subtraction/negation, multiplication, division modulus, a. k. a. remainder exponentiation precedence: Order in which operations are computed. n * / % ** have a higher precedence than + 1 + 3 * 4 is 13 n Parentheses can be used to force a certain order of evaluation. (1 + 3) * 4 is 16 14

Integer division n When we divide integers with / , the quotient is also

Integer division n When we divide integers with / , the quotient is also an integer. 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 n More examples: n n 35 / 5 is 7 84 / 10 is 8 156 / 100 is 1 The % operator computes the remainder from a division of integers. 3 43 4 ) 14 5 ) 218 12 20 2 18 15 3 15

Real numbers n Python can also manipulate real numbers. n n -15. 9997 42.

Real numbers n Python can also manipulate real numbers. n n -15. 9997 42. 0 2. 143 e 17 The operators + - * / % ** ( ) all work for real numbers. n n n Examples: 6. 022 The / produces an exact answer: 15. 0 / 2. 0 is 7. 5 The same rules of precedence also apply to real numbers: Evaluate ( ) before * / % before + - When integers and reals are mixed, the result is a real number. n Example: 1 / 2. 0 is 0. 5 n The conversion occurs on a per-operator basis. n n n 7 / 3 * 1. 2 + 3 / 2 2 * 1. 2 + 3 / 2 2. 4 + 1 3. 4 16

Math commands n n Python has useful commands for performing calculations. Command name Description

Math commands n n Python has useful commands for performing calculations. Command name Description Constant Description abs(value) absolute value e 2. 7182818. . . ceil(value) rounds up pi 3. 1415926. . . cos(value) cosine, in radians floor(value) rounds down log(value) logarithm, base e log 10(value) logarithm, base 10 max(value 1, value 2) larger of two values min(value 1, value 2) smaller of two values round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root To use many of these commands, you must write the following at the top of your Python program: from math import * 17

Python Numbers here are three numeric types in Python: int , float , complex

Python Numbers here are three numeric types in Python: int , float , complex n n Variables of numeric types are created when you assign a value to them: To verify the type of any object in Python, use the type() function: 18

Python Numbers Ø n Int, or integer, is a whole number, positive or negative,

Python Numbers Ø n Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) 19

Python Numbers Ø n Float, or "floating point number" is a number, positive or

Python Numbers Ø n Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example x = 1. 10 y = 1. 0 z = -35. 59 print(type(x)) print(type(y)) print(type(z)) 20

Python Numbers Ø n Complex numbers are written with a "j" as the imaginary

Python Numbers Ø n Complex numbers are written with a "j" as the imaginary part: Example x = 3+5 j y = 5 j z = -5 j print(type(x)) print(type(y)) print(type(z)) 21

Python Numbers n n n n Casting in python is therefore done using constructor

Python Numbers n n n n Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number) float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals Example No. 1 Integers: x = int(1) # x will be 1 y = int(2. 8) # y will be 2 z = int("3") # z will be 3 22

Python Numbers n n n Example No. 2 Floats: x = float(1) # x

Python Numbers n n n Example No. 2 Floats: x = float(1) # x will be 1. 0 y = float(2. 8) # y will be 2. 8 z = float("3") # z will be 3. 0 w = float("4. 2") # w will be 4. 2 Example No. 3 Strings: x = str("s 1") # x will be 's 1' y = str(2) # y will be '2' z = str(3. 0) # z will be '3. 0' 23

print n print : Produces text output on the console. n Syntax: print("Message“) print(Expression)

print n print : Produces text output on the console. n Syntax: print("Message“) print(Expression) n Prints the given text message or expression value on the console, and moves the cursor down to the next line. print (Item 1, Item 2, . . . , Item. N) n n Prints several messages and/or expressions on the same line. Examples: print("Hello, world!“) age = 45 print("You have", 65 - age, "years until retirement“) Output: Hello, world! You have 20 years until retirement 24

input n input : Reads a number from user input. n n You can

input n input : Reads a number from user input. n n You can assign (store) the result of input into a variable. Example: age = input("How old are you? ") print("Your age is", age) age=int(age) print("You have", 65 - age, "years until retirement“) Output: How old are you? 53 Your age is 53 You have 12 years until retirement 25