Download presentation
1
Variables, Expressions and Statements
Python – Part 2
2
Prepared by Department of Preparatory year
Values and Types Values Basic things program works with e.g. letter, number 1, 2, ‘Hello World!’ Types Values belong to different types 2 is an interger ‘Hello World!’ is a string Prepared by Department of Preparatory year
3
Prepared by Department of Preparatory year
Numbers Integers: X1A2 Type ‘int’ Can’t be larger than 2**31(2 31) Octal literals begin with 0 (0981 illegal!) Hex literals begin with 0X, contain 0-9 and A-F Floating point: E E-21 Type ‘float’ Same precision and magnitude as C double Long integers: L Type ‘long’ Any magnitude Python usually handles conversions from int to long Complex numbers: 1+3J Type ‘complex’ Prepared by Department of Preparatory year
4
Prepared by Department of Preparatory year
String >>> print "Per's lecture“ Per's lecture Single quotes or double quotes can be used for string literals Produces exactly the same value Special characters in string literals: \n newline, \t tab, others Triple quotes useful for large chunks of text in program code >>> print "One line.\nAnother line.“ One line. Another line. >>> print """One line, another line.""“ One line, another line. Prepared by Department of Preparatory year
5
Prepared by Department of Preparatory year
Values and Types Print statement for integers >>>print 4 4 Can check the value type >>> type(‘Hello world!’) <type ‘str’> >>> type (17) <type ‘int’> Prepared by Department of Preparatory year
6
Prepared by Department of Preparatory year
Values and Types Strings belog to the type str Integers belong to the type int Numbers with a decimal point belong to the type float. >>>type (3.2) <type ‘float’> Prepared by Department of Preparatory year
7
Prepared by Department of Preparatory year
Values and Types What about ’17’ and ‘3.2’? >>>type (’17’) <type ‘str’> >>>type (‘3.2’) Prepared by Department of Preparatory year
8
Prepared by Department of Preparatory year
Values and Types >>>print 1,000,000 Output ? 1 0 0 Semantic error Prepared by Department of Preparatory year
9
Prepared by Department of Preparatory year
Variables Variable is a name that refers to a value Assignment statement creates new variables and gives them values. >>>message=‘New message’ >>>n=17 >>>pi= Prepared by Department of Preparatory year
10
Prepared by Department of Preparatory year
Variables Use print statement to display the value of a variable >>> print n 17 >>> print message New message Prepared by Department of Preparatory year
11
Prepared by Department of Preparatory year
Variables Type of variable is the type of value it refers to(The type of the variable is determined by Python) >>>type (pi) <type ‘float’> >>>type (n) <type ‘int’> >>>type (message) <type ‘str’> Prepared by Department of Preparatory year
12
Prepared by Department of Preparatory year
Variable Names Can contain both letters and numbers Begin with a letter Good idea to begin variable names with a lowercase letter Underscore character (_) can appear in a name (often in names with multiple words), e.g. my_name The variable name is case sensitive: ‘val’ is not the same as ‘Val’ Prepared by Department of Preparatory year
13
Prepared by Department of Preparatory year
Variable Names >>> 76trombones = 'big parade' SyntaxError: invalid syntax SytaxError: invalid syntax >>>class =‘CS104’ Class -> one of Python’s keywords Prepared by Department of Preparatory year
14
Prepared by Department of Preparatory year
Python Keywords And del from not while As elif global or with Assert else if pass yield Break except import print Class exec in raise Continue finally is return Def for lambda try Prepared by Department of Preparatory year
15
Prepared by Department of Preparatory year
Statements Unit of code that Python interpreter can execute (print, assignment statement) print 1 x=2 print x Prepared by Department of Preparatory year
16
Operators and operands
Operators – special symbols that represent computations (e.g. addition, division) Values the operator is applied to are called operands + addition - subtraction * multiplication / division ** exponentiation % modulus Prepared by Department of Preparatory year
17
Operators and operands
20+32 Hour-1 Hour*60+minute 5**2 (5+9)*(15-7) 7%3 Prepared by Department of Preparatory year
18
Operators and Operands
>>>minute=59 >>>minute/60 0 ? Prepared by Department of Preparatory year
19
Operators and operands
If both operands are integers, result is also an integer If either of the operands is a floating-point number Python performs floatin-point division; result is a float >>>minute/60.0 In Python 3.0 or later the result is a float // operator performs integer division Prepared by Department of Preparatory year
20
Prepared by Department of Preparatory year
Expressions Combination of values, variables and operators 17 X X+17 Prepared by Department of Preparatory year
21
Prepared by Department of Preparatory year
Expressions >>>1+1 2 In a script, expression by itself doesn’t do anything. Prepared by Department of Preparatory year
22
Prepared by Department of Preparatory year
Order of operations Order of evaluation depends on rules of precedence. Python follows mathematical convention Parentheses – highest precedence Exponentiation –next highest precedence Multiplication, Division, Modulus (same precedence) Addition and Subtraction (same prec.) Same precedence operators – left to right Prepared by Department of Preparatory year
23
Prepared by Department of Preparatory year
Order of operations 2*(3-1) (1+1)**(5-2) 2**1+1 3*1**3 2*3-1 6+4/2*3 7%3+8/2 Prepared by Department of Preparatory year
24
Prepared by Department of Preparatory year
String operations Concatenation operator + first =‘CS’ second=‘104’ print first+second Repitition operator * ‘spam’*3 ‘spamspamspam’ Prepared by Department of Preparatory year
25
Prepared by Department of Preparatory year
Comments Notes that can be added to program to explain what the program is doing Start with the # symbol #compute the percentage of the hour that has elapsed percentage=(minute*100)/60 percentage=(minute*100)/60 #percentage of an hour Prepared by Department of Preparatory year
26
Reading Input from the Keyboard
Programs commonly need to read input typed by the user on the keyboard. We will use the Python functions to do this. Python uses built-in functions to read input from the keyboard. A function is a piece of prewritten code that performs an operation and then returns a value back to the program. The input function can be used to read numeric data from the keyboard.
27
Reading Input from the Keyboard
Reading Numbers with the input Function Use the input function in an assignment statement: variable = input (prompt) where, variable name of the variable that will reference the data = assignment operator input name of the function prompt string that is displayed on the screen For example: hours = input (‘How many hours did you work?’)
28
Reading Input from the Keyboard
Reading Strings with the raw_input Function The raw_input function retrieves all keyboard input as a string. >>> name = raw_input(‘Enter your name:’) >>> print name Enter your name: Ahmad
29
Prepared by Department of Preparatory year
Write scripts in IDLE Now we need to write proper scripts, saved in files In IDLE: 'File' 'New Window' Do immediately 'Save as…' Browse to directory 'Desktop' Create a directory 'Python course' Go down into it Enter the file name 't1.py' Save Work in the window called 't1.py' Enter the following code: Save the file: Ctrl-S, or menu 'File', 'Save' Run the script: F5, or menu 'Run', 'Run Module' "file Ex1.py" # this is a documentation string print "Hello world!" Prepared by Department of Preparatory year
30
End Part 2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.