Example: bachelor of science

ภาษาไพทอน (Python language)

(Python language).. 1. (Introduction). 2. (Variable). 3. (Assignment statement). 4. (Data type). 5. (Operator). 6. (Input and output statement). 7.. Computer Science, CMU 204101 Introduction to Computer 1. 1. Introduction .. First program Computer Science, CMU 204101 Introduction to Computer 2.. 1989 Guido van Rossum .. (ABC, Modula-3, C, C++, Algol-68, SmallTalk and Unix shell and other scripting languages) . interface . Computer Science, CMU 204101 Introduction to Computer 3.. 1. Interpreter (process at runtime). 2. interactive . interact . 3.. Computer Science, CMU 204101 Introduction to Computer 4.

''' or """ (triple) ตัวอย่างสตริง word = 'word' sentence = "This is a sentence." นอกจากนยี้ังสามารถใช ้triple (''' or """') เพื่อก าหนดข้อความที่มีหลายบรรทัดได้ เชน่ paragraph = """This is a paragraph ...

Tags:

  Words, Triple

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of ภาษาไพทอน (Python language)

1 (Python language).. 1. (Introduction). 2. (Variable). 3. (Assignment statement). 4. (Data type). 5. (Operator). 6. (Input and output statement). 7.. Computer Science, CMU 204101 Introduction to Computer 1. 1. Introduction .. First program Computer Science, CMU 204101 Introduction to Computer 2.. 1989 Guido van Rossum .. (ABC, Modula-3, C, C++, Algol-68, SmallTalk and Unix shell and other scripting languages) . interface . Computer Science, CMU 204101 Introduction to Computer 3.. 1. Interpreter (process at runtime). 2. interactive . interact . 3.. Computer Science, CMU 204101 Introduction to Computer 4.

2 First program 1 # first program 2 # you can do it 3 print( Welcome to Python! ) # print line of text . 1 comment # comment 2 comment comment 1 . # comment . 3 print () .. Welcome to Python! . Computer Science, CMU 204101 Introduction to Computer 5. python execute 2 mode . (1) INTERACTIVE MODE PROGRAMMING: mode .. (2) SCRIPT MODE PROGRAMMING: editor IDLE (write a simple Python program in a script) . save file file type .py .. source code in a file print Welcome to Python! . This will produce the following result: Welcome to Python! Computer Science, CMU 204101 Introduction to Computer 6. 2. (variable).

3 1. (A-Z a-z) _. (Underscore) . 2. (A-Z a-z) 0-9 .. _ . 3. (Reserved Word). 4.. 5.. NUM , Num num .. Computer Science, CMU 204101 Introduction to Computer 7. Reserved words These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only. and exec not assert finally or break for pass class from print continue global raise def if return del import try elif in while else is with except lambda yield Computer Science, CMU 204101 Introduction to Computer 8.. 123 MyID . _ThinkBig . mylife@CMU Pin number . Dorm_number . Computer Science, CMU 204101 Introduction to Computer 9.

4 3. (assignment statement).. =.. score = 75. gpa = score 75. gpa Computer Science, CMU 204101 Introduction to Computer 10.. multiple assignment . 1. a=b=c=1.. a b c 1. 2. a, b, c = 1, 2, "john".. a 1. b 2. c john Computer Science, CMU 204101 Introduction to Computer 11. 4. (Data type).. (1) int . (2) float . (3) boolean : True, False (4) string . (5) list . Computer Science, CMU 204101 Introduction to Computer 12. Data type : int float (1) int 2 , 50 , 1009. (2) float , Computer Science, CMU 204101 Introduction to Computer 13. Data type : (3) . (Boolean) , True False . 4 > 1 True 6 > 7 False .. Computer Science, CMU 204101 Introduction to Computer 14.

5 Data type : (4) string .. ' (single). " (double). ''' or """ ( triple ).. word = 'word'. sentence = "This is a sentence.". triple (''' or """').. paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""". + * ( ). - . + . - * . Computer Science, CMU 204101 Introduction to Computer 15. Data type : (4) string - (substring) . - [ ] [ : ] . - 0 end-1.. - Hello World! Hello world! 12 . 0 H. 11 end-1 = 12-1 ! Computer Science, CMU 204101 Introduction to Computer 16.. str = 'Hello World!'. print (str) # Prints complete string print (str[0]) # Prints first character of the string print (str[2:5]) # Prints characters starting from 3rd to 5th (end-1 5-1 = 4).

6 Print (str[2:]) # Prints string starting from 3rd character print (str * 2) # Prints string two times print (str + "TEST ") # Prints concatenated string This will produce the following result: Hello World! H. llo llo World! Hello World!Hello World! Hello World!TEST. Computer Science, CMU 204101 Introduction to Computer 17. Data type : (5) (list). - list item list - item (compound data type). - item [ ]. - item , (comma). - list [ ] [ : ]. - item 0 end-1 . item list 0 end-1. myList = [ Hello' , 'my' , student'] . item list 3. item 0. item 2 ( end-1 . 3- 1=2). Computer Science, CMU 204101 Introduction to Computer 18.

7 Exlist1 = ['abcd', 786 , , 'john', ]. exlist2 = ['Hello' ,'my' , 'student']. print (exlist1) # Prints complete list print (exlist1[0]) # Prints first element of the list print (exlist1[1:3]) # Prints elements starting from 2nd till 3rd (end-1 3-1=2). print (exlist1[2:]) # Prints elements starting from 3rd element print (exlist2 * 2) # Prints list two times print (exlist1 + exlist2) # Prints concatenated lists This will produce the following result: ['abcd', 786, , 'john', ]. abcd [786, ]. [ , 'john', ]. ['Hello', 'my', 'student', 'Hello', 'my', 'student']. ['abcd', 786, , 'john', , 'Hello', 'my', 'student'].

8 Computer Science, CMU 204101 Introduction to Computer 19. 5. (Operator). (Arithmetic Operators). (Comparison ( , Relational) Operators). (Assignment Operators). (Logical Operators). Bitwise Operators Membership Operators Identity Operators Computer Science, CMU 204101 Introduction to Computer 20. (Arithmetic Operators). Assume variable a holds 10 and variable b holds 20, then: Operator Description Example Result + Addition a+b 30. - Subtraction a-b -10. * Multiplication a*b 200. / Division b/a 2. % Modulus b%a 0. ** Exponent 2**3 8. // Floor Division 9//2 4. 7/3 # classic division returns a float 17 // 3 # floor division discards the fractional part 5.

9 Computer Science, CMU 204101 Introduction to Computer 21. (Comparison Operators). Assume variable a holds 10 and variable b holds 20, then: Operator Description Example result == equal a == b False != not equal a != b True > greater than a>b False < less than a<b True >= greater than or equal a >= b False <= less than or equal a <= b True Computer Science, CMU 204101 Introduction to Computer 22. (Assignment Operators). Operator Example Explanation = c=a+b += c += a c=c+a -= c -= a c=c-a *= c *= a c=c*a /= c /= a c=c/a %= c %= a c=c%a **= c **= a c = c ** a //= c //= a c = c // a Computer Science, CMU 204101 Introduction to Computer 23.

10 (Logical Operators). Assume variable a holds 10 and variable b holds 20, then: Operator Example Result and a and b True or a or b True not not(a and b) False . P Q P and Q P or Q P Not P. True True True True True False True False False True False True False True False True False False False False Computer Science, CMU 204101 Introduction to Computer 24. Operators . Bitwise Operator : >> , << , & , |. Python Membership Operators : in , not in Python Identity Operators: is , is not Computer Science, CMU 204101 Introduction to Computer 25. (Python Operators Precedence). Operator Description () parentheses ** Exponentiation (raise to the power).


Related search queries