Subject: python script
>>> x = 0
>>> while x < 10: … x += 1 … print x … 1 2 3 4 5 6 7 8 9 10 >>> while x < 10: … print ‘x is currently: ‘, x … x += 1 … else: … print “all done !” … x is currently: 0 x is currently: 1 x is currently: 2 x is currently: 3 x is currently: 4 x is currently: 5 x is currently: 6 x is currently: 7 x is currently: 8 x is currently: 9 all done ! >>> x = 0
>>> while x < 10: … x += 1 … print ‘x is currently: ‘, x … else: … print “all done !” … x is currently: 1 x is currently: 2 x is currently: 3 x is currently: 4 x is currently: 5 x is currently: 6 x is currently: 7 x is currently: 8 x is currently: 9 x is currently: 10 all done ! >>> x = 0
>>> while x < 10: … print ‘x is currently: ‘, x … print ‘x is lesss than 10, adding 1’ … x += 1 … if x == 3: … print “hey x is equals 3 !” … else: … print “continuing….” … continue … x is currently: 0 x is lesss than 10, adding 1 continuing…. x is currently: 1 x is lesss than 10, adding 1 continuing…. x is currently: 2 x is lesss than 10, adding 1 hey x is equals 3 ! x is currently: 3 x is lesss than 10, adding 1 continuing…. x is currently: 4 x is lesss than 10, adding 1 continuing…. x is currently: 5 x is lesss than 10, adding 1 continuing…. x is currently: 6 x is lesss than 10, adding 1 continuing…. x is currently: 7 x is lesss than 10, adding 1 continuing…. x is currently: 8 x is lesss than 10, adding 1 continuing…. x is currently: 9 x is lesss than 10, adding 1 continuing…. >>> range(0,9)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> range(20)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> x = range(0,6)
>>> x
[0, 1, 2, 3, 4, 5]
>>> start = 3
>>> stop = 18
>>> range(start,stop)
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
>>> range(start,stop,3)
[3, 6, 9, 12, 15]
>>> range(start,stop,2)
[3, 5, 7, 9, 11, 13, 15, 17]
>>> range(start,stop,4)
[3, 7, 11, 15]
>>> for num in range(10):
… print num
…
0
1
2
3
4
5
6
7
8
9
>>> for num in range(10):
… print num,
…
0 1 2 3 4 5 6 7 8 9
>>> for num in xrange(10):
… print num,
…
0 1 2 3 4 5 6 7 8 9
>>> for num in xrange(10):
… print num
…
0
1
2
3
4
5
6
7
8
9
>>> x=xrange(1,6)
>>> x2=range(1,6)
>>> x
xrange(1, 6)
>>> x2
[1, 2, 3, 4, 5]
>>> l = [x for x in range(11) if x % 2 == 0]
>>> l
[0, 2, 4, 6, 8, 10]
>>> l = [x for x in xrange(0,11) if x % 2 == 0]
>>> l
[0, 2, 4, 6, 8, 10]
>>> l = [x for x in xrange(0,11) if x % 2 == 1]
>>> l
[1, 3, 5, 7, 9]
>>> l = [x for x in xrange(0,11) if x % 3 == 0]
>>>
>>> l = [x for x in xrange(0,11) if x % 3 == 0]
>>> l
[0, 3, 6, 9]
>>> l = [x for x in xrange(0,11) if x % 3 == 1]
>>> l
[1, 4, 7, 10]
>>> celsius = [0,10,20.1,34.5]
>>> fahrenheit = [(temp * (9/5.0) + 32) for temp in celsius]
>>> fahrenheit
[32.0, 50.0, 68.18, 94.1]
>>> l = [x**2 for x in [x**2 for x in range(11)]]
>>> l
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]
>>> l = [x**4 for x in range(11)]
>>> l
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]
>>> X**2
1048576
>>> l = [x**2 for x in range(8)]
>>> l
[0, 1, 4, 9, 16, 25, 36, 49]
>>> l = [x**2 for x in range(3)]
>>> l
[0, 1, 4]
>>> l = [x**2 for x in range(8)]
>>> l
[0, 1, 4, 9, 16, 25, 36, 49]
>>> l = [x**2 for x in (1,2,4,8,16,32,64.128)]
>>> l
[1, 4, 16, 64, 256, 1024, 4112.400384]
>>> 2**1
2
>>> 2**0
1
>>> l = [2**y for y in range(8)]
>>> l
[1, 2, 4, 8, 16, 32, 64, 128]
>>> 2**8 -2
254
>>> 2**7 -2
126
>>> 2**6 -2
62
>>> 2**5 -2
30
>>> 2**4 -2
14
>>> 2**3 -2
6
>>> 2**2 -2
2
>>> l = [x for x in range(10) if x % 2 == 1]
>>> l
[1, 3, 5, 7, 9]
>>> l = [x for x in range(50) if x % 3 == 0]
>>> l
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]
>>> st = ‘print every word in this stence that has an even number of the letter’
>>> l = len(st)
>>> l
69
>>> st = ‘print every word in this stence that has an even number of the letter’
>>> st.split()
[‘print’, ‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> len(st.split())
14
>>> st.split()
[‘print’, ‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> l = st.split()
>>> l[1]
‘every’
>>> l[1:]
[‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> l[0:]
[‘print’, ‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> l[0:]
[‘print’, ‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> len(l[0:])
14
>>> len(l[0:5])
5
>>> len(l[0])
5
>>> l = range(100)
>>> print l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> st = ‘print every word in this stence that has an even number of the letter’
>>> st.split()
[‘print’, ‘every’, ‘word’, ‘in’, ‘this’, ‘stence’, ‘that’, ‘has’, ‘an’, ‘even’, ‘number’, ‘of’, ‘the’, ‘letter’]
>>> st.split()[0]
‘print’
>>> st.split()[0][0]
‘p’
>>> st.split()[0::][0]
‘print’
>>> st.split()[1][0]
‘e’
>>> st = ‘print only the words that start with s in this sentance’
>>> for w in st.split():
… if w[0] == ‘s’:
… print w
…
start
s
sentance
>>> st = ‘print every word in this sentance that has even number of letter’
>>> for w in st.split():
… if len(w)%2 == 0:
… print w + “—> has an even length !”
…
word—> has an even length !
in—> has an even length !
this—> has an even length !
sentance—> has an even length !
that—> has an even length !
even—> has an even length !
number—> has an even length !
of—> has an even length !
letter—> has an even length !
>>>
>>> def vol(red):
… return (4.0/3)*(3.14)
…
>>> vol
>>> (4.0/3)*(3.14)
4.1866666666666665
>>> def vol():
… return (4.0/3)*(3.14)*(red**3)
…
>>> def ran_check(num,low,high):
… if num in range(low,high):
… print ” %s is in the range” %str(num)
… else:
… print “The number is out the range.”
…
>>> ran_check(3,1,10)
3 is in the range
>>> ran_check(11,1,10)
The number is out the range.
>>>
>>> def ran_check(num,low,high):
… if num in range(low,high):
… print ” %s is in the range” %str(num)
… else:
… print “The number is out the range.”
…
>>> ran_check(11,1,10)
The number is out the range.
>>> ran_check(6,1,10)
6 is in the range
>>>
>>>
>>>
>>> def up_low(s):
… d={“upper”:0, “lower”:0}
… for c in s:
… if c.isupper():
… d[“upper”]+=1
… elif c.islower():
… d[“lower”]+=1
… else:
… pass
… print “Original String : “, s
… print “No. of Upper case characters : “, d[“upper”]
… print “No. of Lower case characters : “, d[“lower”]
…
>>> s = “Hello Mr. Rogers, How are you this fine Tuesday ? ”
>>> up_lows()
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘up_lows’ is not defined
>>> up_low(s)
Original String : Hello Mr. Rogers, How are you this fine Tuesday ?
No. of Upper case characters : 5
No. of Lower case characters : 32
>>>
>>>
>>> def uniq_list(l):
… x = []
… for a in l:
… if a not in x:
… x.append()
… return x
…
>>> uniq_list([1,1,1,1,2,2,2,3,3,3,3,4,5])
>>> def uniq_list(l):
… x = []
… for a in l:
… if a not in x:
… x.append(a)
… return x
…
>>> uniq_list([1,1,1,1,2,2,2,3,3,3,3,4,5])
[1, 2, 3, 4, 5]
>>>
>>> def uniq_list2(l):
… a = set([])
… ret = []
… for x in l:
… if x not in a:
… ret.append(x)
… a.add(x)
… return ret
…
>>> uniq_list2([1,1,1,1,2,2,2,3,3,3,3,4,5])
[1, 2, 3, 4, 5]
>>>
>>>
>>>
>>>
>>> def multiply(numbers):
… total = numbers[0]
… for x in numbers:
… total *= x
… return total
…
>>> multiply([1,2,3,-4])
-24
>>> def plyer_choice(board):
… pass
…
>>> range (1,20)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> range (1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range (0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range (0,-1)
[]
>>> range (0,10)[3:7]
[3, 4, 5, 6]
>>> range (0,10)[-2:]
[8, 9]
>>> range (0,10)[-1:]
[9]
>>> range (0,10)[-1:3]
[]
>>> range (0,10)[-1:-3]
[]
>>> range (0,10)[-1::]
[9]
>>> range (0,10)[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range (0,10)[::]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range (0,10)[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> range (0,10)[::-3]
[9, 6, 3, 0]
>>> class Sample(object):
… pass
…
>>> st = ” Print only the word that start with s in this sentence”
>>> for w in st.split():
… if w[0] == ‘s’:
… print w
…
start
s
sentence
===================================
Function test example:
$ cat test_function_2.py
def sumProblem(x, y):
sum = x + y
sentence = ‘The sum of {} and {} is {}.’.format(x, y, sum)
print(sentence)
def main():
sumProblem(2, 3)
sumProblem(1234567890123, 535790269358)
a = int(input(“Enter an integer: “))
b = int(input(“Enter another integer: “))
sumProblem(a, b)
main()
=================================
Run the script line by line:
$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> def sumProblem(x, y):
… sum = x + y
… sentence = ‘The sum of {} and {} is {}.’.format(x, y, sum)
… print(sentence)
…
>>> def main():
… sumProblem(2, 3)
… sumProblem(1234567890123, 535790269358)
… a = int(input(“Enter an integer: “))
… b = int(input(“Enter another integer: “))
… sumProblem(a, b)
…
>>> main()
The sum of 2 and 3 is 5.
The sum of 1234567890123 and 535790269358 is 1770358159481.
Enter an integer: 45
Enter another integer: 67
The sum of 45 and 67 is 112.
>>>
>>> for l in xrange(1,101):
… if l % 3 == 0 and l % 5 == 0:
… print “FizzBuzz”
… elif l% 3 == 0:
… print “Fizz”
… elif l% 5 == 0:
… print “Buzz”
… else:
… print l,
…