第一天学习python

来源:互联网 发布:触控未来科技 知乎 编辑:程序博客网 时间:2024/04/25 08:30

第一个脚本


print("Hello, world!\n")

print("My name is Jacky Yang. \n\
I am 24 years old. \n\
This is the first time I edit a python file. \nMy favorite sport is basketball game. \
\nMy lover is wwwleslie.")

input()

这里初步学习了最基本的print函数,斜杠加n代表输出换行,在编写代码时想要换行可以用一个斜杠来作为换行符。


第二个脚本


abs(-334)
myString = 'Hello World!'
print(myString)

基本函数abs(),赋值语句

print("%s is is number %i" % ("fdsf", 23)) # %i %d %s %f


#f = open('hah.txt','a')
#print >> f,'bc' #不好使
#f.close()
这个地方以后得回来研究

input() #2.2不懂 好像是python3.5.1版本已经没有raw_input了?


def foo():
    "this is "#文档字符串(具体干什么用的没弄懂
    return True


# + - * / // % ** 加、减、乘、浮点除法、地板除、取余数、乘方


# < <= > >= == !=  标准比较运算符


# and or not 逻辑运算符


# 还要合理使用括号,增强可读性


#命名对大小写字母敏感;动态类型语言:不需预先声明变量的类型;


#增量赋值:n *= 10 和 n = n *10 同义


#没有自增1运算符:没有 ++n


print(0xA0) #十六进制用0x开头


a = 'dsfdsf' #字符串
a[0] #字符串的第一个字符
a[2:3]#第三个字符到第四个字符
a[:2]#从头到第三个字符
# 以上三行是 切片运算


a+a #字符串可以用加号进行连接
a*3 #乘号也行
'-'*20 #可以这么用


'''aa
bb'''
'aa\nbb' #这两个字符串等价(换行


alist = [1,2,3] #列表
atuple = (1,2,3) #元组
#列表和元组都可以进行切片运算


#列表可以被修改,元组不能被修改
alist[2]= 2 #可以运行
atuple[1] = 4 #不能运行




adict = {'abc':'b'}#建立字典(一种映射)
adict['def']=e#向词典添加映射
adict #显示字典中所有映射
for key in adict:print(key,adict[key]) #显示字典中所有映射(与上一行中的区别是:显示格式不同)


adict.keys()#显示字典中的映射前半部分


#代码块的逻辑用缩进表示,而不是大括号(所以需要手工排版?


#if
n1 = input('输入一个数字n1=')#输入一个数字
if n1<0:
    print("n1小于0")
elif n1>0:
    print("n1大于0")
elif n1 == 0:
    print("n1等于0")
else:
    print("你输入的不是数字!")




#while
counter=0
while counter <3:
    print('hah this is %d' % counter)# 也可以是 print('hah this is',counter)
    counter +=1


for aa in ['fsf','sfs','asfa']:
    print(aa),
    print


who = 'players'
what = 'Go!'
print('We are',who,'. We say',what,what,what)
print('We are %s. We say %s' % (who, (what + ' ')*4))#输出的两种表示方法


for saf in [' ds', 'sfs' 'fs', 'fs', 'sdf']:#空格分割的表示在同一行,逗号分开的表示换行
    print(saf)
for saf in range(3):
    print(saf)


for num in [2,322,12,32]:
    print(num)


foo = 'afsf'
for s in foo:#迭代字符
    print(s)


for b in range(len(foo)):#字符串索引
    print(foo[b])


for i,c in enumerate(foo):#索引和元素同时索引

    print(i,c)


到这里《python核心编程(第二版)》学习到了2.13,该学习2.14列表解析了

0 0
原创粉丝点击