Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. Algebraic expressions >>> 2+3 5 >>> (3+1)*4 16 >>> 2**3 8 >>> 17%4 1 >>> 17//4 4 >>> min(2,3) 2 >>> max(2,6,3,4) 6 Boolean expressions >>> 1 < 3 True >>> 3 < 1 False >>> 3 == 3 True >>> 3 != 3 False >>> 2+3 == 5 True >>> (2+3) == 5 True Boolean operators >>> 2 < 3 and 3 < 4 True >>> 2 == 4 or 3 < 4 True >>> not (2==4) True Variables and assignment statement >>> x = 3 >>> x 3 >>> x**2 9 >>> y = x**2 >>> y 9 >>> x3_A = 3 >>> x3_A 3 Strings >>> s = 'hello!' >>> s 'hello!' >>> t = 'world' >>> t 'world' >>> s == t False String operators >>> s + t 'hello!world' >>> s = 'hello' >>> t = 'world' >>> s == t False >>> s + t 'helloworld' >>> s + ' ' + t 'hello world' >>> 3*s 'hellohellohello' >>> 3*(s+' '+t+'!\n') 'hello world!\nhello world!\nhello world!\n' >>> res = 3*(s+' '+t+'!\n') >>> print(res) hello world! hello world! hello world! >>> res 'hello world!\nhello world!\nhello world!\n' >>> '!' in res True >>> len(res) 39 >>> s in res True >>> 'll' in res True >>> s 'hello' >>> t 'world' >>> t[0] 'w' >>> t[1] 'o' >>> t[2] 'r' >>> t[-1] 'd' >>> t[-2] 'l' >>> t[-3] 'r' Lists and list operators >>> lst = ['ant', 'bat', 'cat', 'dog'] >>> lst[0] 'ant' >>> lst[1] 'bat' >>> lst[-1] 'dog' >>> 'ant' in lst True >>> 'elk' in lst False >>> 3*lst ['ant', 'bat', 'cat', 'dog', 'ant', 'bat', 'cat', 'dog', 'ant', 'bat', 'cat', 'dog'] >>> lst + lst ['ant', 'bat', 'cat', 'dog', 'ant', 'bat', 'cat', 'dog'] >>> min(lst) 'ant' >>> max(lst) 'dog' >>> lst ['ant', 'bat', 'cat', 'dog'] >>> lst[1] = 'bee' >>> lst ['ant', 'bee', 'cat', 'dog'] >>> s 'hello' >>> s[1] = 'a' Traceback (most recent call last): File "", line 1, in s[1] = 'a' TypeError: 'str' object does not support item assignment List methods >>> lst ['ant', 'bee', 'cat', 'dog'] >>> lst.append('eel') >>> lst ['ant', 'bee', 'cat', 'dog', 'eel'] >>> lst.count('dog') 1 >>> lst.remove('dog') >>> lst ['ant', 'bee', 'cat', 'eel'] Function help() >>> help(lst) Help on list object: class list(object) | list() -> new empty list | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | ... | | append(...) | L.append(object) -> None -- append object to end | | clear(...) | L.clear() -> None -- remove all items from L | | copy(...) | L.copy() -> list -- a shallow copy of L | | count(...) | L.count(value) -> integer -- return number of occurrences of value | | extend(...) | L.extend(iterable) -> None -- extend list by appending elements from the iterable | | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. | | insert(...) | L.insert(index, object) -- insert object before index | | pop(...) | L.pop([index]) -> item -- remove and return item at index (default last). | Raises IndexError if list is empty or index is out of range. | | remove(...) | L.remove(value) -> None -- remove first occurrence of value. | Raises ValueError if the value is not present. | | reverse(...) | L.reverse() -- reverse *IN PLACE* | | sort(...) | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* | ... Modules >>> import math >>> help(math) Help on module math: NAME math MODULE REFERENCE http://docs.python.org/3.3/library/math The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(...) acos(x) Return the arc cosine (measured in radians) of x. acosh(...) acosh(x) Return the hyperbolic arc cosine (measured in radians) of x. asin(...) asin(x) Return the arc sine (measured in radians) of x. ... trunc(...) trunc(x:Real) -> Integral Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method. DATA e = 2.718281828459045 pi = 3.141592653589793 FILE /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload/math.so >>> math.sqrt(4) 2.0 >>> math.pi 3.141592653589793 >>> math.sin(math.pi) 1.2246467991473532e-16 Module turtle >>> import turtle >>> s = turtle.Screen() >>> t = turtle.Turtle() >>> t.forward(100) >>> t.left(90) >>> t.forward(50) >>> t.pensize(3) >>> t.color('red') >>> t.circle(50,180) >>> t.goto(0,0) >>> t.undo() >>> t.penup() >>> t.goto(0,0) >>> t.pendown() >>> t.forward(20) >>> t.setheading(180) >>> t.circle(50) >>> s.bye() Writing programs helloworld.py >>> ================================ RESTART ================================ >>> hello world! input.py >>> ================================ RESTART ================================ >>> Please type your name: Ljubomir Hello, Ljubomir! oneway.py >>> ================================ RESTART ================================ >>> Please type your name: Ljubomir Enter your age: 46 Ljubomir is eligible to vote twoway.py >>> ================================ RESTART ================================ >>> Please type your name: Ljubomir Enter your age: 46 Ljubomir is eligible to vote Bye! >>> ================================ RESTART ================================ >>> Please type your name: Marlena Enter your age: 13 You will be eligible to vote in 5 years Bye! For loop and funtion range() >>> for i in range(10): print(i) 0 1 2 3 4 5 6 7 8 9 >>> for c in 'Ljubomir': print(c) L j u b o m i r >>> lst = [5,3,1] >>> for num in lst: print(num**2) 25 9 1 >>> for i in range(3,10): print(i) 3 4 5 6 7 8 9 >>> for i in range(3,10,2): print(i) 3 5 7 9 >>> for i in range(10,3,-2): print(i) 10 8 6 4 >>> lst [5, 3, 1] >>> for i in range(len(lst)): print(lst[i]) 5 3 1 while loop >>> while True: name = input('Enter your name: ') print('Hello',name) Enter your name: Ljubomir Hello Ljubomir Enter your name: Ellie Hello Ellie Enter your name: Lena Hello Lena Enter your name: Traceback (most recent call last): File "", line 2, in name = input('Enter your name: ') KeyboardInterrupt >>> User-defined funtions functions.py >>> ================================ RESTART ================================ >>> >>> help(f) Help on function f in module __main__: f(x) my first function >>> f(2) 17 >>> ================================ RESTART ================================ >>> >>> avg(17,24) 20.5 >>> ================================ RESTART ================================ >>> >>> lst = [4,2,6,3,1,5,8] >>> isSorted(lst) False >>> lst.sort() >>> isSorted(lst) True File I/O functions.py >>> ================================ RESTART ================================ >>> >>> copy('test.txt', 'new.txt') >>> infile = open('test.txt') >>> infile.read(1) 'F' >>> infile.read(1) 'i' >>> infile.read(1) 'r' >>> infile.read(5) 'st Li' >>> infile.readline() 'ne.\n' >>> infile.read() '\nThird Line.\n' >>> infile.close() >>> outfile = open('new.txt', 'w') >>> outfile.write('First line...') 13 >>> outfile.write('Still first line...') 19 >>> outfile.write('\nSecond and ... \nThird.\n') 24 >>> outfile.close() >>> infile = open('new.txt') >>> print(infile.read()) First line...Still first line... Second and ... Third. >>> infile.close() Accessing WWW content >>> from urllib.request import urlopen >>> hekp(urlopen) Traceback (most recent call last): File "", line 1, in hekp(urlopen) NameError: name 'hekp' is not defined >>> help(urlopen) Help on function urlopen in module urllib.request: urlopen(url, data=None, timeout=, *, cafile=None, capath=None, cadefault=False) >>> response = urlopen('http://reed.cs.depaul.edu/lperkovic/cs4hs2013/session.txt') >>> type(response) >>> response.getURL() Traceback (most recent call last): File "", line 1, in response.getURL() AttributeError: 'HTTPResponse' object has no attribute 'getURL' >>> response.getUrl() Traceback (most recent call last): File "", line 1, in response.getUrl() AttributeError: 'HTTPResponse' object has no attribute 'getUrl' >>> response.geturl() 'http://reed.cs.depaul.edu/lperkovic/cs4hs2013/session.txt' >>> for header in response.getheaders(): print(header) ('Server', 'Apache-Coyote/1.1') ('Accept-Ranges', 'bytes') ('ETag', 'W/"923-1375326995000"') ('Last-Modified', 'Thu, 01 Aug 2013 03:16:35 GMT') ('Content-Type', 'text/plain') ('Content-Length', '923') ('Date', 'Thu, 01 Aug 2013 12:14:26 GMT') ('Connection', 'close') >>> raw = response.read() >>> raw b'pi@raspberrypi ~ $ ls\nDesktop Documents ocr_pi.png python_games Scratch\npi@raspberrypi ~ $ pwd\n/home/pi\npi@raspberrypi ~ $ ls ..\npi\npi@raspberrypi ~ $ ls /\nbin dev home lost+found mnt proc run selinux sys usr\nboot etc lib media opt root sbin srv tmp var\npi@raspberrypi ~ $ cd Documents/\npi@raspberrypi ~/Documents $ ls\nScratch Projects\npi@raspberrypi ~/Documents $ mkdir Python\\ Projects/\npi@raspberrypi ~/Documents $ ls\nPython Projects Scratch Projects\npi@raspberrypi ~/Documents $ cd Python\\ Projects/\npi@raspberrypi ~/Documents/Python Projects $ ls\npi@raspberrypi ~/Documents/Python Projects $ nano test.txt\npi@raspberrypi ~/Documents/Python Projects $ ls\ntest.txt\npi@raspberrypi ~/Documents/Python Projects $ rm test.txt \npi@raspberrypi ~/Documents/Python Projects $ nano test.txt\npi@raspberrypi ~/Documents/Python Projects $ ls\ntest.txt\npi@raspberrypi ~/Documents/Python Projects $ \n' >>> text = raw.decode() >>> text 'pi@raspberrypi ~ $ ls\nDesktop Documents ocr_pi.png python_games Scratch\npi@raspberrypi ~ $ pwd\n/home/pi\npi@raspberrypi ~ $ ls ..\npi\npi@raspberrypi ~ $ ls /\nbin dev home lost+found mnt proc run selinux sys usr\nboot etc lib media opt root sbin srv tmp var\npi@raspberrypi ~ $ cd Documents/\npi@raspberrypi ~/Documents $ ls\nScratch Projects\npi@raspberrypi ~/Documents $ mkdir Python\\ Projects/\npi@raspberrypi ~/Documents $ ls\nPython Projects Scratch Projects\npi@raspberrypi ~/Documents $ cd Python\\ Projects/\npi@raspberrypi ~/Documents/Python Projects $ ls\npi@raspberrypi ~/Documents/Python Projects $ nano test.txt\npi@raspberrypi ~/Documents/Python Projects $ ls\ntest.txt\npi@raspberrypi ~/Documents/Python Projects $ rm test.txt \npi@raspberrypi ~/Documents/Python Projects $ nano test.txt\npi@raspberrypi ~/Documents/Python Projects $ ls\ntest.txt\npi@raspberrypi ~/Documents/Python Projects $ \n' >>> print(text) pi@raspberrypi ~ $ ls Desktop Documents ocr_pi.png python_games Scratch pi@raspberrypi ~ $ pwd /home/pi pi@raspberrypi ~ $ ls .. pi pi@raspberrypi ~ $ ls / bin dev home lost+found mnt proc run selinux sys usr boot etc lib media opt root sbin srv tmp var pi@raspberrypi ~ $ cd Documents/ pi@raspberrypi ~/Documents $ ls Scratch Projects pi@raspberrypi ~/Documents $ mkdir Python\ Projects/ pi@raspberrypi ~/Documents $ ls Python Projects Scratch Projects pi@raspberrypi ~/Documents $ cd Python\ Projects/ pi@raspberrypi ~/Documents/Python Projects $ ls pi@raspberrypi ~/Documents/Python Projects $ nano test.txt pi@raspberrypi ~/Documents/Python Projects $ ls test.txt pi@raspberrypi ~/Documents/Python Projects $ rm test.txt pi@raspberrypi ~/Documents/Python Projects $ nano test.txt pi@raspberrypi ~/Documents/Python Projects $ ls test.txt pi@raspberrypi ~/Documents/Python Projects $ >>>