Python学习笔记-DAY1

目录

基础加减乘除

变量的使用

字符串输出

字符串的连接

数组的使用

While循环

多行语句表示一行

三重引号可以用于跨越多个行的字符串

Python注释:

需要敲回车结束:

分号( ; ) 允许在单行写入多条语句

多个语句组作为套件:

输入name

元组与列表

字典(通过键值对的形式进行存储)

python 的所有数据类型都是类,可以通过 type() 查看该变量的数据类型:

  • Python数据类型转换

总结

 

基础加减乘除

>>> 17 / 3  # int / int -> int

5

>>> 17 / 3.0  # int / float -> float

5.666666666666667

>>> 17 // 3.0  # explicit floor division discards the fractional part

5.0

>>> 17 % 3  # the % operator returns the remainder of the division

2

>>> 5 * 3 + 2  # result * divisor + remainder

17

 

>>> 5 ** 2  # 5 squared

25

变量的使用

>>> width = 20

>>> height = 5 * 9

>>> width * height

900

 

字符串输出

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print '"Isn\'t," she said.'
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print, \n is included in the output
'First line.\nSecond line.'
>>> print# with print, \n produces a new line
First line.
Second line.

 

字符串的连接

 

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'

 

 

>>> 'Py' 'thon'
'Python'

 

>>> prefix + 'thon'
'Python'

 

数组的使用

list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]

tinylist = [123, 'john']

print list # 输出完整列表

print list[0] # 输出列表的第一个元素

print list[1:3] # 输出第二个至第三个的元素

print list[2:] # 输出从第三个开始至列表末尾的所有元素

print tinylist * 2 # 输出列表两次

(123, 'john', 123, 'john')

print list + tinylist # 打印组合的列表

 

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'

 

 

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

 

 

>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]

 

>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']

 

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

 

While循环

... a, b = 0, 1
>>> while b < 10:
...     print b
...     a, b = b, a+b
...
1
1
2
3
5
8

 

 

多行语句表示一行:使用续行字符(\)包含在[]{}()括号内的陈述并不需要使用续行符

item_one='py'
item_two='th'
item_three='on'
total=item_one +\
      item_two+\
      item_three
print total

1

2

days = ['Monday', 'Tuesday', 'Wednesday',

    'Thursday', 'Friday']

 

Python接受单引号('),双引号()和三(''”“”)引用,以表示字符串常量,三重引号可以用于跨越多个行的字符串

paragraph="""This is a paragraph. it is
made up of multiple lines and sentences"""

print paragraph

 

Python注释:

一个井号(#),这不是一个字符串文字开头的注释。“#”号之后字符和到物理行是注释的一部分,Python解释器会忽略它们。

1

2

3

#!/usr/bin/python

# First comment

print "Hello, Python!"; # second comment

这将产生以下结果:

1

Hello, Python!

 

需要敲回车结束:

raw_input("\n\nPress the enter key to exit.")

 

分号( ; ) 允许在单行写入多条语句

1

import sys; x = 'foo'; sys.stdout.write(x + '\n')

 

 

多个语句组作为套件:(不需要写括号,只需要同一个块儿的代码缩进相同)

if expression :

  suite

elif expression :

  suite

else :

  suite

 

输入name

name=raw_input('please input your name')
print'hello,',name

 

元组与列表

tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )

list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]

tuple[2] = 1000 # 元组中是非法应用 (元组不允许修改)

list[2] = 1000 # 列表中是合法应用

 

 

字典(通过键值对的形式进行存储)

dict={}
dict[
'one']="This is one"
dict[2]="This is two"
tinydict={'name':'liu','code':1234,'dept':'sales'}
print dict['one']
print dict[2]
print tinydict['name']
print tinydict.keys()
print tinydict.values()

python 的所有数据类型都是类,可以通过 type() 查看该变量的数据类型:

>>> n=1

>>> type(n)

<type 'int'>

>>> n="runoob"

>>> type(n)

<type 'str'>

>>>

 

a = 111

isinstance(a, int)

True

 

  •  type()不会认为子类是一种父类类型。
  •  isinstance()会认为子类是一种父类类型。
  • Python数据类型转换

int(x [,base])

将x转换为一个整数

long(x [,base] )

将x转换为一个长整数

float(x)

将x转换到一个浮点数

complex(real [,imag])

创建一个复数

str(x)

将对象 x 转换为字符串

repr(x)

将对象 x 转换为表达式字符串

eval(str)

用来计算在字符串中的有效Python表达式,并返回一个对象

tuple(s)

将序列 s 转换为一个元组

list(s)

将序列 s 转换为一个列表

set(s)

转换为可变集合

dict(d)

创建一个字典。d 必须是一个序列 (key,value)元组。

frozenset(s)

转换为不可变集合

chr(x)

将一个整数转换为一个字符

unichr(x)

将一个整数转换为Unicode字符

ord(x)

将一个字符转换为它的整数值

hex(x)

将一个整数转换为一个十六进制字符串

oct(x)

将一个整数转换为一个八进制字符串

 

总结:

1. 数据类型 分为数字型和非数字型。

数字型包括整型,长整型,浮点型,复数型;

非数字型包括字符串,列表,元组和字典

非数字型的共同点:都可以使用切片、链接(+)、重复(*)、取值(a[])等相关运算;

非数字型的不同点:

列表 可以直接赋值,元组不可以赋值,字典按照 dict[k]=v 的方式赋值。

2 (1)Numbers(int long float complex (a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型)

(2)String

(3)List  []

(4)Tuple(元祖)(),相当于只读列表,不可以二次赋值

(5)dictionary(字典){},key值对

 

3 主提示符 >>>

  从属提示符…

猜你喜欢

转载自blog.csdn.net/yige__cxy/article/details/81157396