Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Scripting Languages Python basics Interpreter ● direct conversation (terminal command: python) ● version, help, modules ● experiments with numbers and variables Python Python 2.7.12 2.7.12 (default, (default, Jul Jul 11 2016, 2016, 15:12:24) 15:12:24) [GCC [GCC 5.4.0 5.4.0 20160609] 20160609] on on linux2 linux2 Type Type "help", "help", "copyright", "copyright", "credits" "credits" or or "license" "license" for more information. for more information. >>> >>> 2+2 2+2 44 >>> >>> 2.0+2.0 2.0+2.0 4.0 4.0 >>> >>> 2**32 2**32 4294967296 4294967296 >>> >>> aa == 10 10 >>> >>> a**2 a**2 100 100 Numbers ● No declaration needed ● Numerical data types: – integer “int” ● ● ● – float ● – ● 64bit → ±9,223372,036854,775807 or “long” for very big ones for integer division always use // use dot: 1.0, 2.0, 0.1, etc. complex (you will not need this now) Type check: – type() ● Mixing types in calculation ● Automatic type conversion >>> >>> 1024*1024 1024*1024 1048576 1048576 >>> >>> 2**64 2**64 18446744073709551616L 18446744073709551616L >>> >>> 3/2 3/2 11 >>> >>> 3//2 3//2 11 >>> >>> 3.0/2.0 3.0/2.0 1.5 1.5 >>> >>> (1+1j)*(1-1j) (1+1j)*(1-1j) (2+0j) (2+0j) >>> >>> xx == 17 17 -- 2.0 2.0 >>> >>> type(x) type(x) <type <type 'float'> 'float'> Numerical type conversion ● Use int(), float() for changing the type – int() → float() – float() → int() fraction will be lost ! >>> >>> aa == 33 >>> >>> 11 ++ float(a) float(a) 4.0 4.0 >>> >>> bb == 1.5 1.5 >>> >>> 11 ++ int(b) int(b) 22 >>>float(int(3.3)) >>>float(int(3.3)) 3.0 3.0 Names, Assignment, Expression x_1=(10 + y)*3.0 name assignment expression ● ● Name( Variable ) case-sensitive – must start with a letter – space and ()[]{}+-/*” are not allowed Assignment operator: single “=” – ● aa == 22 – multiple assignments allowed Expression – values, variables, operators – operators priority: **, -, */,+- – brackets ii == jj == kk == 0.0 0.0 x, x, y, y, zz == 1, 1, 2, 2, 33 X, X, yy == y, y, xx -2**2 ≠ (-2)**2 Exercise ● Temperature conversion: – variables: c, k, f (Celsius, Kelvin, Fahrenheit) cc == 77 kk == ... ... ff == ... ... print print c, c, k, k, ff Strings ● Data type: a sequence of characters – Single quotes: 'string', 'may contain double" ' – Double quotes: "string", "may contain single' " ● Escape: '\'', "\"", – triple quotes '''…''' or """…""", may include single&double quotes and newlines (\n) aa bb cc dd ee == == == == == "123" "123" '1+2+3' '1+2+3' "12\"35'" "12\"35'" '12"35\'' '12"35\'' """12"35""" """12"35""" aa == ''' ''' this this can can be be any any long long text text in in many many lines lines ''' ''' String properties ● Operations on strings: help(str) – len(s) – s1 + s2 – s.isdigit() – s.isalpha() – s.upper() – s.lower() – and others >>> >>> aa == "Hello" "Hello" >>> >>> len(a) len(a) 55 >>> >>> a.isdigit() a.isdigit() False False >>> >>> a.upper()+a.lower() a.upper()+a.lower() 'HELLOhello' 'HELLOhello' >>> >>> type(a) type(a) <type <type 'str'> 'str'> String slicing ● ● Numbered sequence – string – position s="Hello" 01234 Substrings: [ ] – first char at position 0 – last char at position -1 – access range with ":" ● 0:n means n chars, – from 0 to n-1 position >>> >>> xx == "Hello "Hello world" world" >>> >>> x[0] x[0] 'H' 'H' >>> >>> x[0:4] x[0:4] 'Hell' 'Hell' >>> >>> x[:4] x[:4] 'Hell' 'Hell' >>> >>> x[:] x[:] 'Hello 'Hello world' world' >>> >>> x[-1] x[-1] 'd' 'd' >>> >>> x[3:-1] x[3:-1] 'lo 'lo worl' worl' >>> >>> x[-4:-1] x[-4:-1] 'orl' 'orl' >>> >>> x[-4:] x[-4:] 'orld' 'orld' String formatting ● Gluing together strings and numbers – string.format("pattern",args) – {n} for argument placement and position – d,f,s for int, float and string types >>> >>> aa == "I'm "I'm {0} {0} and and >>> >>> print print aa I'm I'm John John and and I'm I'm 20 20 >>> >>> print print "T={0:2d}C "T={0:2d}C T= T= 1C 1C is is +274.1500K +274.1500K I'm I'm {1}".format('John',20) {1}".format('John',20) is is {1:+.4f}K".format(1,274.15) {1:+.4f}K".format(1,274.15) https://docs.python.org/2/library/string.html#format-specification-mini-language Logic data type: bool ● Only two values: – ● True or False Operators: – comparisons: ● – ==, !=, <, <=, >, >= logic operators: ● not, and, or >>> >>> 1==2 1==2 False False >>> >>> True==1 True==1 True True >>> >>> a,b,c a,b,c == 1,2,3 1,2,3 >>> >>> a<0 a<0 or or b>c b>c and and False False >>> >>> a<0 a<0 or or b<c b<c and and True True >>> >>> dd == a<0 a<0 or or b<c b<c >>> >>> dd True True >>> >>> type(d) type(d) <type <type 'bool'> 'bool'> c>2 c>2 c>2 c>2 and and c>2 c>2 Comments ● Comments start with hash ‘#’ character ● ‘#’ must be the first (non-white) character ● Everything after ‘#’ is ignored till the end of line ● Use comments in your code ! ## ww ww == –– width width 10 10 ## ll ll == –– length length 20 20 ## aa aa == –– ww area area ** ll Taking decisions → if-elif-else – conditions are bool-type followed by ‘:’ – body instructions are indented (use 4-spaces) if cond : instr if if a>0: a>0: print print else: else: print print if cond : instr1 else: instr2 "positive" "positive" "not "not positive" positive" if cond1 : instr1 elif cond2 : instr3 else: instr3 if if a>0: a>0: print print elif elif a<0: a<0: print print else: else: print print "positive" "positive" "negative" "negative" "zero" "zero" Conditional loop → while – condition bool-type in the first line ● – in Python there is no loop with condition at the end (nothing like do-while exists) body indented while cond : instr while while ii jj ii == == << j: j: i+1 i+1 j-1 j-1 Exercise - factorial – Factorial n! = n∙(n-1)∙...2∙1 ● ● ● how far you can go with value of n? what is the data type of result ‘s’? how many digits has 100! start n = … s = n False print s stop n>1 True n = n-1 s = s*n Loop control →break, continue ● break →immediate escape from the loop ● continue →immediate start of the next cycle while cond1 : if cond2: break instr aa == 00 while while aa << 10: 10: aa == a+1 a+1 if if a**4 a**4 >> 1000: 1000: break break print print a, a, a**4 a**4 while cond1 : if cond2: break instr aa == 11 11 while while aa >> -10: -10: aa == a-1 a-1 if if a==0: a==0: continue continue print print a, a, 1.0/a 1.0/a Exercise – guest-it game start goal=… goal goal == …… while while True: True: read readguess guess if if …… print print elif elif …… print print else else print print break break True True guess …… False …… guess>goal True "too big" …… guess<goal "got it" stop True "too big" Getting input ● s = raw_input("prompt") – print a "prompt" message and return input – always the string-type is returned – s.isdigit() → make sure there are digits only – n = int(s) → convert to integer while while True True ss == raw_input("Number raw_input("Number -> -> ") ") if if not not s.isdigit(): s.isdigit(): continue continue nn == int(s) int(s) …… Random values ● Add a library (module) to your program – ● Use function from the module – ● import random random.function() Read documentation – Python 2 Standard Library ● 9. Numeric and Mathematical Modules – 9.6. random — Generate pseudo-random numbers ● random.randint(a, b) Return a random integer N such that a <= N <= b import import random random …… guess guess == random.randint(1,100) random.randint(1,100) Math functions ● Add a library (module) to your program – ● import math Read documentation – Python 2 Standard Library ● 9. Numeric and Mathematical Modules – 9.2. math — Mathematical functions import import math math …… xx == math.sqrt(math.cos(2*math.pi/T)**2 math.sqrt(math.cos(2*math.pi/T)**2 -1) -1)