Python-Day-01 Python入门指导

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-Day-01 Python入门指导相关的知识,希望对你有一定的参考价值。

  • Python介绍

    • Python优缺点

优点:易学、可移植性、解释型语言、面向对象、丰富的库

缺点:强制缩进、速度不如C、无法加密、不能使用多线程

    • Python版本

python2.X python3.x

python 2.7兼容版本,停止更新库

python3.x新版本,目前流行版本,持续更新维护库

    • Python安装

windows安装:https://www.python.org/getit/下载安装python,注意解决环境变量问题,继续安装Pycharm客户端,进行python编译调试

linux:自带python,可能需更新版本


 

  • Python编程入门

  • 字符集介绍

ASCII--->GB2312--->GBK1.0--->GBK18030--->Unicode--->UTF-8

  • 第一个python程序

1 #!/usr/bin/env python
2 print("hello world!")
  •  定义变量

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 name = "panda"
4 print(name)

 

  • input输入 

1 #!/usr/bin/env python
2 name = input("yourname:")
3 print("your name is:",name)

 技术分享

  • 代码注释

注释掉单行使用#

注释掉多行使用

‘‘‘
name = input("myname:")
print("my name is:",name)
‘‘‘
  •  多行输入及数据带入

‘‘‘   ‘‘‘   多行输入,文字拼接
%s 字符代入
{_name}.format(_name=name) 字符带入
技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 name = input("Name:")
 4 job = input("Job:")
 5 age = input("Age:")
 6 info = ‘‘‘-----info of %s-------
 7 Name:%s
 8 Job:%s
 9 Age:%s
10 ______________________
11 ‘‘‘%(name,name,job,age)
12 print(info)
input_%s
技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 name = input("Name:")
 4 job = input("Job:")
 5 age = input("Age:")
 6 info = ‘‘‘-----info of {_Name}-------
 7 Name:{_Name}
 8 Job:{_Job}
 9 Age:{_Age}
10 _______________________________
11 ‘‘‘.format(_Name=name,_Job=job,_Age=age)
12 print(info)
input_format

 

技术分享

  • if条件语句

简易输入用户名密码进行对比

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 _username = "panda"
 4 _passwd = "123456"
 5 username = input("Username:")
 6 password = input("Password:")
 7 if username == _username and password == _passwd:
 8     print("welcome to you!")
 9 else:
10     print("there are errror username or password!")
if_else

技术分享技术分享

定义数值,多次比较

int() 数据类型转换为int

myage = 23   对应    guessage = int(input("guess age:"))

myage = "23"  对应   guessage = input("guess age:")

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 guessage = int(input("guess myage:"))
 5 if myage == guessage:
 6     print("you got it!")
 7 elif guessage > myage:
 8     print("think smaller!")
 9 else:
10     print("you should think bigger!")
if_elif_else

 

技术分享技术分享

  • while条件语句

1. while true

猜测数值,猜对退出,N次错误退出

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while True:
 6     if count > 2:
 7         break
 8     guessage = int(input("guess myage:"))
 9     if myage == guessage:
10         print("you got it!")
11         break
12     elif guessage > myage:
13         print("think smaller!")
14     else:
15         print("you should think bigger!")
16         count +=1
while_true

技术分享技术分享

2.while+条件

whiile count < 3:

         ........

else:

   print()

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while count < 3:
 6     guessage = int(input("guess myage:"))
 7     if myage == guessage:
 8         print("you got it!")
 9         break
10     elif guessage > myage:
11         print("think smaller!")
12     else:
13         print("you should think bigger!")
14         count +=1
15 else:
16     print("you have error too many times!")
while_条件_else

技术分享

3.while+条件+if 多次判断

猜错三次,询问是否继续,继续count重置为0,不继续 break 提示good bye

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while count < 3:
 6     guessage = int(input("guess myage:"))
 7     if myage == guessage:
 8         print("you got it!")
 9         break
10     elif guessage > myage:
11         print("think smaller!")
12     else:
13         print("you should think bigger!")
14         count +=1
15         if count == 3:
16             continue_file = input("are you want to continue??")
17             if continue_file != "n":
18                 count = 0
19             else:
20                 print("good bye!")
21                 break
while_条件_if_else_if_else

 

技术分享

  • for条件语句

1.for循环 打印I的值

for i in range(x,y,z)

x 起始值

y 结束值

z 间隔值

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(10):
4     print("look:",i)

 

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(1,10,2):
4     print("look:",i)

技术分享技术分享

2.for循环三次,猜数字,三次后退出打印信息

技术分享
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 for i in range(3):
 5     guessage = int(input("guess age:"))
 6     if guessage == myage:
 7         print("you got it!")
 8         break
 9     elif guessage > myage:
10         print("think smaller!")
11     else:
12         print("think bigger!")
13 else:
14     print("you have error many times!")
for_if

技术分享技术分享

  • break/continue区别对比

break 退出整个循环

continue 退出当前循环

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(10):
4     if i < 5:
5         print("look:",i)
6     else:
7         print("he he he")
8         #continue
9         break

技术分享技术分享




以上是关于Python-Day-01 Python入门指导的主要内容,如果未能解决你的问题,请参考以下文章

Python-Day01-task

python-day01

python-day01

Python-Day-01 Python入门指导

python零基础入门第二讲

为啥不在 Python 中导入转换器?