2015年10月14日 星期三

Python學習筆記

1. Display python version:
python -V

2. Python help:
python
>>>help('len')  ## help for 'len'
>>>help()         ## enter help mode
help>return      ## help for 'return'
...................
help>q              ## return to python mode
>>>

3. Comments
In python are using # as comment symbol.

4. Single Quote '' and Double Quotes ""
Strings in double quotes work exactly the same way as strings in single quotes.
You can use triple quotes - (""" or ''') for specify multi-line strings. An example is:
''' This is a multi-line string. This is first line.
This is second line.
"How are you?," I asked.
He said "I'm fine."
'''

5. Strings are Immutable
Can not change string after created.

6. Concat strings in python
Example:
age = 20
name = 'Wayne'
print '{0} was {1} years old'.format(name, age)
print 'Why is {0} playing with that python?'.format(name)

print name + ' is ' + str(age) + ' years old too'

print 'My name is', name


Output:
Wayne was 20 years old when he wrote this book
Why is Wayne playing with that python?

Wayne is 20 years old too
My name is Wayne

 7. Special char in strings
Can use backreferences \ or raw string: r"This is change line indicated \n"

8. If elif else
number = 23
guess = int(raw_input('Enter an integer:'))

if guess == number:
    print 'Guess it'
elif guess < number:
    print 'No, it is a litter higher than that'
else:
    print 'No, it is a little lower than that'

print 'Done'

8. While
number = 23
running = True
while running:
    guess = int(raw_input('Enter an integer : '))
    if guess == number:
        print 'Congratulations, you guessed it.'
        running = False
    elif guess < number:
        print 'No, it is a little higher than that.'
    else:
        print 'No, it is a little lower than that.'
else:
    print 'The while loop is over.'

print 'Done'


9. For
for i in range(1, 5):
    print i
else:
    print 'The for loop is over'

10. Function
def say_hello(a):
    # block belonging to the function
    print 'hello world', a

# End of function


a = 'test'
b = 'happy'
say_hello(a) # call the function
say_hello(b) # call the function again


11. global statement and local scope
x = 50
y = 10
def func(input):
    global x          ## use global value 50, otherwise will be local scope

    print 'input is', input
    y = 100
    print 'x is', x

    print 'y is', y
    x = 2
    print 'Changed global x to', x


func(y)
print 'Value of x is', x

print 'Value of y is', y

Output:
input is 10
x is 50
y is 100
Changed global x to 2
Value of x is 2
Value of y is 10

12. Default argument values
def say(message, times=1):
    print message * times
say('Hello')
say('World', 5)


Output:
Hello
WorldWorldWorldWorldWorld

13. Keyword arguments
def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)

#func(b=10)  ## Add this line will compile error, should be removed

Output:
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

14. Varargs parameters
def total(initial=999, *numbers, **keywords):
    count = initial
    print 'count is', count
    for number in numbers:
        print 'number is', number
        count += number
        print '[number]count is', count
    for key in keywords:
        print 'keywords[key] is', keywords[key]
        count += keywords[key]
        print '[key]count is', count
    return count
print total(10, 1, 2, 3, vegetables=50, fruits=100) ##Cannot use
total(10, 1, test=9, 3, vegetables=50, fruits=100) or  total(10, 1, 3, vegetables=50, fruits=100, 99) will compile error

Output:
count is 10
number is 1
[number]count is 11
number is 2
[number]count is 13
number is 3
[number]count is 16
keywords[key] is 50
[key]count is 66
keywords[key] is 100
[key]count is 166
166

15.



沒有留言:

張貼留言