- I have planed to learn Python for many times.
- I have started to learn Python for many times .
- However, I can't use it fluently up to now.
- Now I know I was wrong.
- I just tried to remember the syntax and took notes on paper with little practice.
- Now I print the Python Tutorial and learn it by using it.
- Practice. Practice. Practice.
- I love Python. It is glamourous.
Here are notes:
# In interactive mode, the last printed expression is assigned to the variable _
>>> price * tax
7.5
>>> price + _
113.0625
# use raw string
>>> print 'C:\some\name'
C:\some
ame
>>> print r'C:\some\name'
C:\some\name
# use triple-quotes:
>>> print """\ # \ is to prevent \n
Usage: thingy [OPTIONS]
-h
-H hostname
"""
>>> 3 * ('pyth' 'on')
'pythonpythonpython'
# slice
>>> word = 'Python'
>>> word[ :2]
'Pyt'
>>> word[0] = 'J' # error. You can't do this.
>>> len(word)
6
# use Unicode strings.
>>> u'Hello\u0020World!'
u'Hello World'
>>> ur'Hello\\u0020world'
>>> u'Hello\\\\u0020world'
# list
>>> squares = [1, 'abc', 9, 16, 25]
>>> squares[1][1]
'b'
>>> squares.append("abcd")
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
# Fibonacci series:
while b < 10:
print b
a, b = b, a+b
# Control Flow
# if
if x < 0:
x = 0
elif x == 0:
x = 1
else:
print 'Ok'
# for - 1
words = ['cat', 'window', 'defenestrate']
for w in words:
print w, len(w)
# for - 2
for w in words[:]:
if len(w) > 6:
words.insert(0, w)
# here is a question: I use 'for w in words' while that can't make sense. Why?
>>> range(0, 10, 3)
[0, 3, 6, 9]
a = ['a', 'b', 'c']
for i in range(len(a)):
print i, a[i]
# you can alse use:
for index, item in enumerate(a):
print index, item
# break & continue & else on LOOP:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
# The else belongs to the 'for' loop, not the 'if' statement.
else:
# loop fell through without finding a factor
print n, 'is a prime number'