CODECADEMY



字符串索引

The string "PYTHON" has six characters,
numbered 0 to 5, as shown below:

+---+---+---+---+---+---+
| P | Y | T | H | O | N |
+---+---+---+---+---+---+
0 1 2 3 4 5

So if you wanted "Y", you could just type
"PYTHON"[1] (always start counting from 0!)

Strings & Console Output

len() str() lower() upper()

Let's take a closer look at why you use len(string) and str(object), but dot notation (such as "String".upper()) for the rest.

lion = "roar"
len(lion)
lion.upper()
Methods that use dot notation only work with strings.

On the other hand, len() and str()can work on other data types.

str()

print "The value of pi is around " + 3.14 会报错 cannot concatenate 'str' and 'float' objects(和java貌似不太一样)

print "The value of pi is around " + str(3.14)即可

占位符的使用

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)


The datetime Library

from datetime import datetime 

导入之后
datetime.now()获取当前时间

now = datetime.now()
print now
print now.year
print now.month
print now.day

获取当前年月日

from datetime import datetime
now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)


###Conditionals & Control Flow

**代表指数 3**2=9

Boolean operators aren't just evaluated from left to right. Just like with arithmetic operators, there's an order of operations for boolean operators:
not is evaluated first;
and is evaluated next;
or is evaluated last.
尽量别指望这个,用括号。

x = "J123"
x.isalpha()  # False

此方法检测字符串是否为纯字母

Pig Latin Game

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word+first+pyg
    new_word = new_word[1:len(new_word)]
    print new_word
else:
    print 'empty'

注意字符串切割方法,new_word[a:b]含头不含尾

Functions

#函数调用函数
def cube(number):
    return number ** 3

def by_three(number):
    if number % 3 == 0 :
        return cube(number)
    else:
        return False

导包

A module is a file that contains definitions—including variables and functions—that you can use once it is imported.

import math 
print math.sqrt(25) #Generic Imports

from math import sqrt  #注意这里不需要括号
print sqrt(25)          #Function Imports

from math import *    
                                #Universal Imports

Universal imports may look great on the surface, but they're not a good idea for one very important reason: they fill your program with a ton of variable and function names without the safety of those names still being associated with the module(s) they came from.容易混淆变量/函数名的来源

import math            # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything       # Prints 'em all!

注意dir()的使用

内建的一些函数

maximum = max(-1, -2, -3 , -4, 5, 6, 7)
print maximum  # 7

minimum = min (-1, -2, -3 , -4, 5, 6, 7)
print minimum  # -4

bsolute = abs(-42)
print absolute # 42

print type(42)        #<type 'int'>     type()函数用来获取变量的类型
print type(4.2)       #<type 'float'>
print type('spam') #<type 'str'>
def distance_from_zero(s):
    if type(s) is int or type(s) is float:
        return abs(s)
    else:
        return 'Nope'     #最后的review,随便尝试了一下居然可以这样写...简直吊

 posted on 2015-05-05 13:40  絮状颗粒  阅读(236)  评论(0)    收藏  举报