Some Python codes
Everything is an object
look at a sample of code… assignments, arithmetics, if, print
Basic Datatypes
Python Build-in types
Single- and Double-Quoted

注脚

展开查看详情

1.Understand ing the Basics

2.PRINTING

3.Some Python codes • 6+9 • Print 6+9 • Print “6+9” • aa=6+9 print aa • aa=6 bb=9 aa+bb

4. Print • Python command print – Displays data, values, and expressions • The single and double quotation mark string objects, which are collections of texts surrounded by quotes.

5.Be familiar with Python printing >>> print ("Hello, World! ") Hello, World! >>> "hello" 'hello' >>> "world" 'world' >>> "hello"+"world" 'helloworld'

6.Be familiar with Python printing >>> "hello" * 3 'hellohellohello' >>> "hello" * 300

7. Variables • Are not declared, just assigned • The variable is created the first time you assign it a value • Are references to objects • Type information is with the object, not the reference • Everything in Python is an object

8.Variables • variable: A named piece of memory that can store a value. • Usage: • 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. • Syntax: name = value • Examples: x = 5 gpa = 3.14 x 5 gpa 3.14 • 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. 8

9. Everything is an object • Everything means >>> x = 7 everything, including >>> x functions and classes 7 (more on this later!) >>> x = 'hello' >>> x 'hello' >>> • Data type is a property of the object and not of the variable

10.Look at a sample of code… Look at a sample of code… assignments, arithmetics, if, print x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “ World” # String concat. print x print y

11.Declare a variable and assign its value Example: Area of a rectangle >>> width = 20 >>> height = 45 >>> area = width * height >>> area 900

12.Evaluation of expressions in Python >>> print ("Hello, World! ") Hello, World! >>> 10 + 25 35 >>> 124 – 125 -1 >>> 1/3 0.333333333333333

13. Print of long lines • print : Produces text output on the console. • Syntax: print "Message" print Expression • Prints the given text message or expression value on the console, and moves the cursor down to the next line. print Item1, Item2, ..., ItemN • Prints several messages and/or expressions on the same line. • Example: print "Hello, world!" age = 45 print "You have", 65 - age, "years until retirement" Output: Hello, world! You have 20 years until retirement 13

14. Enough to Understand the Code • Assignment uses = and comparison uses ==. • For numbers +, -, *, /, % are as expected. • Special use of + for string concatenation. • Special use of % for string formatting. • Logical operators are words (and, or, not) not symbols (&&, ||, !). • The basic printing command is “print.” • First assignment to a variable will create it. • Variable types don’t need to be declared. • Python figures out the variable types on its own.

15.Basic Datatypes • Integers (default for numbers) z = 5 / 2 # Answer is 2, integer division. • Floats x = 3.456 • Strings Can use “ ” or ‘ ’ to specify. “abc” ‘abc’ (Same thing.) thing Unmatched ones can occur within the string. “matt’s” Use triple double-quotes for multi-line strings or strings than contain both ‘ and “ inside of them: “““a‘b“c”””

16. Comments • Start comments with # – the rest of line is ignored. • Can include a “documentation string” as the first line of any new function or class that you define. • The development environment, debugger, and other tools use it: it’s good style to include one. def my_function(x, y): “““This is the docstring. This function does blah blah blah.””” # The code would go here...

17.Python Build-in types • Numbers 3.1415 • Strings “Hello World” • Lists [1,2,3,4] • Dictionaries {‘test’: ‘yum’} • Files input=open(‘file.txt’, ‘r’)

18.TEST: What are the outputs? Why? >>> a=3 >>> b=4 >>> a+1 >>> b*3 >>> b/3 >>> b/3.0 >>> b**2 >>> 2+4.0 >>> 2.0**b

19.Hello World Program

20.Hello World Program • Implement by three different languages • In C • In JAVA • In Python

21.“Hello World” in C main() { printf("hello, world\n"); }

22.“Hello World” in C #include <iostream> using namespace std; main() { cout<<“Hello World” ; }

23.“Hello World” in JAVA class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); } }

24.“Hello World” in Python print "hello World!"

25.“Hello World” in Python print (“Hello World!!”)

26. Your first Python program- hello.py # This program says hello and asks for my name. hello = 'Hello world! ' print(hello) print('What is your name?') myName = input() print('It is good to meet you, ' + myName)

27.hello.py executed sequentially # This program says hello and asks for my name. Any text following a # sign is a comment. Comments are not # # sign is a comment. Comments are not sign is a comment. Comments are not for the computer, but for you, the programmer. # sign is a comment. Comments are not 1.hello = 'Hello world! ' # assign the string to a name 2.print(hello) # call the print( ) function 3.print('What is your name?') # call the print( ) function 4.myName = input() # call the input( ) function 5.print('It is good to meet you, ' + myName)

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

29.Integer division • When we divide integers with / , the quotient is also an integer. 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 • More examples: • 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 29