您的位置:首页 > 编程语言 > Python开发

python(the first week)

2017-10-18 23:13 281 查看
一、Python的属于解释型语言。

     编译型:一次性,将全部的程序编译成二进制文件,然后再运行。

                   优点:运行速度快。

                    缺点:开发效率低,不能跨平台。

       解释型:当你的程序运行时,一行一行的解释,并运行。

                     优点:调试代码很方便,开发效率高,并且可以跨平台。

                      缺点:运行速度慢

二、关于安装



三、



四、变量

       将一些运算结果存在内存,以便后续的程序使用。

  1、数字,字母,下划线的任意组合。

  2、不能以数字开头。

  3、不能是Python中的关键字。

    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',

     'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass',

    'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

  4、可描述性。

    name = 'lixiao'

    age = 18

    #驼峰体:AgeOfOldboy = 48

    #下划线:age_of_oldboy = 48(推荐)

  5、不要中文,不要拼音,不能太长

    √  ① __ = 'polkop'

    ×  ① 1ab =  'ppp'     ② _*   =  'ooo'    ③  -_ = 'lll' 

 五、常量

  常量全部用大写表示

  ID = '37018119981011900293'

  BIR_OF_CHINA = 1949

 六、字符串

  运算

  1、 s1 = '123'

         s2 = 'poi'

    s3 = s1 + s2

  2、 name = 'come on'

    s = name * 3

    print(s)

    ###come oncome oncome on

  3、

name = input('请输入你的名字:')
age= input('请输入你的年龄:')
s1 = '我的名字是:'
s2 = '我的年龄是:'
print(s1 + name,s2 + age)


  4、三种方式

  ① 'adew'  ② "qwqe"  ③ '''poijnsjfi'''

七、用户交互input

  input(全是字符串)

八、type(查看数据类型的)

  name = input('lixiao')

  type(name)### class(str)

九、将字符串转化成数字  int(str)

补充占位符(day2)

msg = '我叫%s,今年%s,我学习进度为1%%'('sunv',18)

print(msg)

    1、例:

name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入工作:')
hobbie = input('请输入爱好:')

msg = '''
----------info of %s----------
name  :%s
age   :%d
job   :%s
hobbie:%s
---------end----------------
'''%(name,name,int(age),job,hobbie)
print (msg)


  #格式化输出     %占位符    s:str字符串    d:数字(digit)

  #int(str)  只有当str全为数字时才可转化。

  2、

  


十、if...else...

  1、  

score = int(input('请输入分数:'))

if score == 100 :
print('666啊,满分啊')
elif score >= 90:
print('不错了,A')
elif score >= 80:
print('不错了,b')
elif score >= 70:
print('不错了,c')
elif score >= 60:
print('不错了,d')
else:
print('完蛋')


  2、比较

   


  3、嵌套

name = input('请输入你的名字:')
if name == 'xiaoxiao':
if True:
print('美女')
else:
print('还OK啦')
else:    print('我不认识她哎')


十一、while循环

1、
flag = True
print('1111')
while flag:
print('我')
print('Monica')
print('风继续吹')
flag = False
print('6666')
print(flag)
print('2222')


2、break(day2补充)



print('1111')
while True:
print('红')
print('缘分')
print('沉默是金')
break
print('当年情')
print('2222')


②打印 1-100

count = 1
while True:
print(count)
count += 1
if  count > 100:
break


3、continue(day2补充)

①虽是死循环,但当爱已成往事和2222,永远不会输出。

print('1111')
while True:
print('想你')
print('明月夜')
print('春光乍泄')
continue
print('当爱已成往事')
print('2222')


②打印1--10(除去7)

count = 1
while True:
count += 1
if count == 7:
continue
else:
print(count)
if count == 10:
break


也可改变条件

count = 0
while count < 10
count += 1
if count == 7
continue
else:
print(count)


4、(day2补充)

while ...else...(如果 while 遇到 break 就不执行 else )

        如果 while False:

.......###不会执行

     else:

.......###会执行



count = o
while count <= 5:
count += 1
print('loop',count)
else:
print('循环正常执行完')
print('------end------')   ###结果Loop3\nLoop4\nLoop5\nLoop6\n循环正常执行完




count = 0
while count <= 5:
count += 1
print('Loop',count)
if count == 3:
break
else:
print('循环完成')
print('-------out of while loop-----------')######Loop 1\nLoop2\nLoop3\n---------out of while loop---------


-----------------------------------------------------------------------------


一、1-10(除去7)

改①:

count = 1
while count < 11:
if count == 7:
print('')
else:
print(count)
count += 1
②
count = 0
while count<10:
count += 1
if count == 7:
continue
else:
print(count)


二、1-100的和

count = 0
sum = 0
while count < 101:
sum += count
count += 1
print(sum)


三、1-100奇数的和

count = 1
while count<101:
if count % 2 ==1:
print(count)
count += 1


四、1-100的偶数

count = 1
while count<101:
if count % 2 ==0:
print(count)
count += 1

五、1-2+3-4.......+99

count = 1
sum = 0
while count < 100:
if count % 2 == 1:
sum += count
else:
sum -= count
count += 1
print(sum)


六、用户输入三次,若不对再给三次机会(升级版)

name = 'sunv'
pwd = 'lx'
count = 2
c = 0
whil
b156
e count >= 0:
username = input('请输入用户名:')
password = input('请输入密码:')
if username == name and password == pwd:
print('欢迎%s登陆系统!'%username)
break
else:
print('对不起,输入错误,你还有%d次机会'%count)
count -= 1
c += 1
if count < 0 and c<6:
n = input('是否再试试?是请输入:1,否请输入2。')
if n == '1':
count = 2
else:
print('退出系统')


#########################################################


一一、

最开始:ASCII码只有8位,1字节,并且第一位全为0

后又发明了万国码Unicode,给中国16位,两字节,不够      后又变成4字节,用不了    后升级为   utf-8  ,三字节

查看当前Python中表示Unicode字符串时占用的空间:import sys    如果是 65535 表示使用ucs2标准(2字节);1114111则ucs4

后国内发明gbk,每个占两个字符通用性不好

二二、关于基本运算符

1、运算符

+ - * / % ** //

2、比较运算

==   !=(<>)   >     <      >=      <=

3、赋值运算

=    +=   -+    *=    /=     %=     **=     //=

4、转换:

① int与bool值之间的转换

#1,bool ----> int
a = int(True)
b = int(False)
print(a,b)  ###   1,0
#int ---> bool
a = bool (93479)
b = bool (0)
c = bool (-1)
print(a,b,c)#####  True   False  True

② str与bool值之间的转换

str-->bool值

s = 'sunv'
s1 = ' ' #空格
s2 = ''
print(bool(s),bool(s1),bool(s2))
### True True False # 非空字符串都为 True
bool值-->str
a = str(True)
b = str(False)
c = str(2 > 1) ### 注意下哈,是True!!!
print(a,type(a),b,type(b),c,type(c))
###True <class 'str'> False <class 'str'> True <class 'str'>

5、逻辑运算(优先级 not>and>or)

and    or    not 

①###  x or y 如果 x 为真,则值为x,否则为 y

print(4 or 3)
print(2 or 3)
print(0 or 3)
print(-1 or 3)###  4  2  3   -1
print(3 or 3 > 2)  ###   3
print(3 > 2 or 3)  ###   True
②### x and y 如果 x 为真,则值为y,否则为 x

③in       not  in

s1 = 'abcd'
print('a' in s1)
print('abc' in s1)
print('ac' in s1)    ###  True     True    False
print(1 and 'a' in s1)    ###   True ###先算'a' in s1


### 评论 出现 苹果、bug 及 sunv 提示 

a = '苹果'
b = 'bug'
s = 'sunv'
p = input('请输入评论:')
if a in p or b in p or s in p:
print('您输入的评论非法!')
else:
print('评论成功!')



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 语言