Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.

Similar presentations


Presentation on theme: "Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set."— Presentation transcript:

1 Introduction to Python Dr. José M. Reyes Álamo

2 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set of instructions that solve a problem and can be executed on a computer Rule 3: The best way to improve your programming and problem solving skills is to practice.

3 3 Computer Programs A program is a sequence of instructions. To run a program is to: –Create the sequence of instructions according to your design and the language rules –Turn that program into the binary code that the computer understands –Give the binary code to the Operating System (OS), so it can pass it to the processor for execution –OS and processor together, execute the program

4 4 Interpreted Python is an interpreted language Interpreted means that Python looks at each instruction, one at a time, and translate it into machine language. –That allows you to enter instructions one-at-a-time. You can also create a file with several instructions called a script, load it, and execute all the instruction one after the other. To rerun an script, reload it.

5 Interactive mode vs scripting mode

6 6 Two ways to work: Interactive and scripting One of the benefits of working with an interpreted language is that you can test bits of code in interactive mode before you put them in a script. But there are differences between interactive mode and script mode that can be confusing.

7 7 Interactive mode For example, if you are using Python as a calculator, you might type >>> miles = 26.2 >>> miles * 1.61 42.182

8 8 Scripting mode But if you type the same code into a script and run it, you will get no output. In script mode an expression, has no visible effect. Python actually evaluates the expression, but it doesn’t display the value unless you tell it to do so: miles = 26.2 print miles * 1.61

9 Calculating area and circunference

10 10 Code Example The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Aaddison-Wesley. All rights reserved

11 11 Import of Math One thing we did was to import the math module with import math This imported python math statements We precede all operations of math with math.OPERATION – e.g. math.pi, math.pow(x, y)

12 12 Getting Input The function: input(“Give me a value”) Prints “Give me a value” on the python screen and waits until the user types anything and presses Enter After pressing enter, the result is stored as a string

13 13 Assignment Statements The ‘=‘ is the assignment statement The value to the right is associated with the variable name on the left It does not stand for equality!

14 14 Convert from string to integer To do math, Python requires converting the sequence of characters into an integer

15 15 Printing Output myVar = 12 print (‘My var has a value of:’,myVar) print takes a list of elements to print, separated by commas –If the element is a string, prints it as is –If the element is a variable, prints the value associated with the variable –After printing, moves on to a new line of output

16 16 Save as a script When you save a file with a.py extension, it becomes a python script You can run the script from the IDLE to see the results of the operation A script is just a file with several python commands Python provides many scripts or modules for common tasks such as math, databases, networking, etc.

17 17 Errors!!! If Python interpreter does not understand your code you will get errors Check the syntax and fix it You can them import the program again until there are no more errors

18 18 Common Error When using IDLE, if you save a file without a.py extension, it will not colorize and format the file. Resave with the.py extension

19 19 Statements Statements are commands. –They perform some action n = n + n ** 3 n = 3 print ‘Hello!’

20 20 Expressions Expressions perform some operation and have a value –Typically do not modify values in the interpreter 3 n + 3 n ** 3

21 21 Comments Code is what actually executes Comments do not execute but they make the programs easier to understand, improving readability. n + 3 #this an expression n = n ** 3 #this is an statement print ‘Hello!’ #this is another statement

22 22 Literals Literal is a programming notation for a fixed value. Integer numbers are literals because they have fixed values –e.g. 123, 999, 1000.

23 23 Operators Integer –Addition and subtraction: +, - –Multiplication: * –Division (quotient): / –Remainder: % Floating point –add, subtract, multiply, divide: +, -, *, /

24 24 Variables A variable is a location in memory where we store data in our program. We assign names to variables to make our program more readable.

25 25 Variables Python maintains a list of pairs for every variable: –variable’s name –variable’s value A variable is created when a value is assigned the first time. It associates a name and a value Subsequent assignments update the associated value. We say the variable name references the value A variable type depends on the value assigned to it. NameValue X7 X = 7

26 26

27 27 Python Naming Conventions Must begin with a letter or an _ (underscore) – Ab123 and _myData are OK, but 123ABC is not. May contain letters, digits, and underscores – this_is_an_identifier_123 May be of any length Upper and lower case letters are different – LengthOfRope is not the same as lengthofrope Variables starting with _ have special meaning.

28 28 When = Doesn’t Mean Equal It might look confusing at first to see the following expression: – myInt = myInt + 7 Here the = doesn’t mean equal, but assignment.

29 29 = sign means assignment In many computer languages the = means assignment. – myInt = myInt + 7 – lhs = rhs What assignment means is: –Evaluate the expression on the right-hand-side of the equal sign –Take the resulting value and store it in the variable indicated on the left-hand-side Only variables can go into the left-hand side of an assignment statement

30 30 More on Assignment Example: x = 3 * 5 + 2 –evaluate expression (3*5 + 2) = 17 –change the value of x to 17 Example (if y has value 2): y = y + 3 –evaluate the expression (y+3) = 2+ 3 = 5 –change the value of y to 5

31 31 Variables and Types Python does not require you to pre-define the data type of a variable The data type of a variable can change Nonetheless, knowing the data type can be important for using the correct operation on a variable. Thus proper naming is important!

32 32 What is a Type? A Python data type essentially defines two things: –The internal structure of the type (what it contains) –The operations you can perform on it. You can capitalize letter, and add numbers –You cannot capitalize numbers or add letter.

33 33 Python Data Types Integer: 5 Float: 1.2 Boolean: True, False String: “anything” or ‘something’ List: [,]: [‘a’,1,1.3] Others

34 34 Fundamental Types Integers – 1, -27 Floating Point – 3.14, 10.0,.001, 3.14e-10, 0e0 Boolean (True or False values) – True, False (notice the use of caps)

35 35 Converting Types A character ‘1’ is NOT the same as the integer 1 You need to convert the value returned by the input command (characters) into an integer int(“123”) yields the integer 123

36 36 Type Conversion int(someVar) converts to an integer float(someVar) converts to a float str(someVar) converts to a string should check out what works: –int(2.1)  2, int(‘2’)  2, but int(‘2.1’) fails –float(2)  2.0, float(‘2.0’)  2.0, float(‘2’)  2.0, float(2.0)  2.0 –str(2)  ‘2’, str(2.0)  ‘2.0’, str(‘a’)  ‘a’

37 37 Types and Division Python does binary operations on two values of the same type, yielding a value of that type: 2/3, integer types, yield integer (0). – 2%3 is the remainder, an integer (2) 2.0/3.0, float types, yield float (0.66666)

38 38 Mixed Types 4/3 results is 1 under integer division 4.0/3.0 results in 1.3333333 under float division What is the result of 4/3.0? –Python will automatically convert to the most detailed type. Thus 4  4.0, and the result is 1.3333333


Download ppt "Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set."

Similar presentations


Ads by Google