Ò». Python ±äÁ¿ÀàÐÍ
#ÕûÐÍ
integer_number = 90
#¸¡µã
float_number = 90.4
#¸´Êý
complex_number = 10 + 10j
#list ÐòÁУºÁÐ±í¡¢Ôª×éºÍ×Ö·û´®¶¼ÊÇÐòÁÐ,ÐòÁеÄÁ½¸öÖ÷ÒªÌØµãÊÇË÷Òý²Ù×÷·ûºÍÇÐÆ¬²Ù×÷·û¡£
sample_list = [1,2,3,'abc']
#dictionary ×Öµä
sample_dic = {"key":value, 2:3}
#tuple Ö»¶ÁµÄÐòÁÐ
sample_tuple = (1,3,"ab")
¶þ. Python ³ÌÐòÁ÷³Ì¿ØÖÆ
2.1 Ìõ¼þÅжϽṹ
flag1 = some_value
flag2 = other_value
if flag1:
do_function_1()
elif flag2:
do_function_2()
else:
do_function_3()
2.2 Ñ»·½á¹¹
for i in range(0, 10):
print(i)
for i in ['a','b','c','dd','eee'];
print(i)
Èý. Print º¯Êý¼°¸ñʽ»¯Êä³ö
3.1 Print ×Ô¶¯»»ÐÐ
ÔÚPython 3.0 ÒÔºó£¬Python ÔÚprint º¯ÊýÉÏ×öÁËÐ޸ġ£ ÔÚPython 2.x °æ±¾ÖУ¬Ê¾ÀýÈçÏ£º
for i in range(0,5):
print i
ĬÈÏÇé¿öÊÇ×Ô¶¯»»Ðеģ¬Èç¹û˵ÊDz»×Ô¶¯»»ÐУ¬ÔÚ×îºó¼Ó¶ººÅ¾Í¿ÉÒÔÁË:print i,
ÔÚPython 3.0 µÄÎĵµÀ¶Ôprint ˵Ã÷ÈçÏ£º
print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no object is given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used.
ÔÚPython 3.x °æ±¾ÖУ¬Èç¹û²»Ïë×Ô¶¯»»ÐУ¬¾ÍÐèҪʹÓÃend ²ÎÊý¡£¸Ã²ÎÊýĬÈÏʹÓÃ'\n',¼´»Ø³µ»»ÐУ¬Èç¹û²»ÏëʹÓ㬻»³ÉÆäËû×Ö·û£¬»òÕßΪ¿Õ¼´¿É¡£ ʾÀýÈçÏ£º
>>> for i in range(5):
print(i,end='')
01234
>>> for i in range(5):
print(i)
0
1
2
3
4
>>> for i in range(5):
print(i,end=',')
0,1,2,3,4,
3.2 print Õý³£Êä³ö
ʹÓÃprintÊä³ö¸÷Ð͵Ä
£¨1£©. ×Ö·û´®
£¨2£©. ÕûÊý
£¨3£©. ¸¡µãÊý
£¨4£©. ³ö¶È¼°¾«¶È¿ØÖÆ
>>> print(str);
tianlesoftware oracle dba
>>>
3.3 ¸ñʽ»¯Êä³öÕûÊý
python printÒ²Ö§³Ö²ÎÊý¸ñʽ»¯£¬ÓëCÑÔµÄprintfËÆ£¬ ʾÀý£º
>>> str = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print(str)
the length of (Hello World) is 11
»òÕßÖ±½ÓдµÀprintÀ
>>> print( "the length of (%s) is %d" %('Hello World',len('Hello World')))
the length of (Hello World) is 11
>>>
3.4 ¸ñʽ»¯Êä³ö16ÖÆÕûÊý
nHex = 0x20
#%x --- hex Ê®Áù½øÖÆ
#%d --- dec Ê®½øÖÆ
#%o --- oct °Ë½øÖÆ
ʾÀý£º
>>> nHex = 0x20
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = 20,nDec = 32,nOct = 40
3.5 ¸ñʽ»¯Êä³ö¸¡µãÊý(float)
#µ¼Èëmath Ä£¿é
>>> import math
#default
>>> print("PI = %f" % math.pi)
PI = 3.141593
#width = 10,precise = 3,align = left
>>> print("PI = %10.3f" % math.pi)
PI = 3.142
#width = 10,precise = 3,align = rigth
>>> print("PI = %-10.3f" % math.pi)
PI = 3.142
#Ç°ÃæÌî³ä×Ö·û
>>> print("PI = %06d" % int(math.pi))
PI = 000003
>>>
3.6 ¸ñʽ»¯Êä³ö×Ö·û´®(string)
#precise = 3
>>> print("%.3s " % ("jcodeer"))
jco
#precise = 4
>>> print("%.*s" % (4,"jcodeer"))
jcod
#width = 10,precise = 3
>>> print ("%10.3s" % ("jcodeer"))
jco
3.7 Êä³öÁбí(list)
#listÖ±½Ó´òÓ¡¼´¿É
>>> l = [1,2,3,4,'tianlesoftware']
>>> print(l)
[1, 2, 3, 4, 'tianlesoftware']
>>> print(l[0])
1
>>> print(l[4])
tianlesoftware
3.8 Êä³ö×Öµä(dictionary)
>>> dave = {1:'A',2:'B',3:'C',4:'D'}
>>> print(dave)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
>>> print(dave[4])
D
>>> print(dave[1])
A