day01——python从认识开始

Python是什么:

  首先,Python 是一门编程语言。这里是Python 的官网: https://www.python.org

  下面是维基百科的解释:

  Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象、直译式的计算机程序语言,具有近二十年的发展历史。它包含了一组功能完备的标准库,能够轻松完成很多常见的任务。它的语法简单,与其它大多数程序设计语言使用大括号不一样,它使用缩进来定义语句块。

  与Scheme、Ruby、Perl、Tcl等动态语言一样,Python具备垃圾回收功能,能够自动管理内存使用。它经常被当作脚本语言用于处理系统管理任务和网络程序编写,然而它也非常适合完成各种高级任务。Python虚拟机本身几乎可以在所有的作业系统中运行。使用一些诸如py2exe、PyPy、PyInstaller之类的工具可以将Python源代码转换成可以脱离Python解释器运行的程序。

  Python的官方解释器是CPython,该解释器用C语言编写,是一个由社区驱动的自由软件,目前由Python软件基金会管理。

  Python支持命令式程序设计、面向对象程序设计、函数式编程、面向侧面的程序设计、泛型编程多种编程范式。

Python可以干什么:

  Web程序
  Python经常被用于Web开发。比如,通过mod_wsgi模块,Apache可以运行用Python编写的Web程序。使用Python语言编写的Gunicorn作为Web服务器,也能够运行Python语言编写的Web程序。Python定义了WSGI标准应用接口来协调Http服务器与基于Python的Web程序之间的沟通。一些Web框架,如Django、Pyramid、TurboGears、Tornado、web2py、Zope、Flask等,可以让程序员轻松地开发和管理复杂的Web程序。

  Python对于各种网络协议的支持很完善,因此经常被用于编写服务器软件、网络蠕虫。第三方库Twisted支持异步在线编写程序和多数标准的网络协议(包含客户端和服务器),并且提供了多种工具,被广泛用于编写高性能的服务器软件。另有gevent这个流行的第三方库,同样能够支持高性能高并发的网络开发。

  GUI开发
  Python本身包含的Tkinter库能够支持简单的GUI开发。但是越来越多的Python程序员选择wxPython或者PyQt等GUI包来开发跨平台的桌面软件。使用它们开发的桌面软件运行速度快,与用户的桌面环境相契合。通过PyInstaller还能将程序发布为独立的安装程序包。

  操作系统
  在很多操作系统里,Python是标准的系统组件。大多数Linux发行版和Mac OS X都集成了Python,可以在终端机下直接运行Python。有一些Linux发行版的安装器使用Python语言编写,比如Ubuntu的Ubiquity安装器、Red Hat Linux和Fedora的Anaconda安装器。Gentoo Linux使用Python来编写它的Portage软件包管理系统。Python标准库包含了多个调用作业系统功能的库。通过pywin32这个第三方软件包,Python能够访问Windows的COM服务及其它Windows API。使用IronPython,Python程序能够直接调用.Net Framework。

  其他
  NumPy、SciPy、Matplotlib可以让Python程序员编写科学计算程序。有些公司会使用Scons代替make构建C++程序。

  很多游戏使用C++编写图形显示等高性能模块,而使用Python或者Lua编写游戏的逻辑、服务器。相较于Python,Lua的功能更简单、体积更小;而Python则支持更多的特性和数据类型。很多游戏,如EVE Online使用Python来处理游戏中繁多的逻辑。

  YouTube、Google、Yahoo!、NASA都在内部大量地使用Python。OLPC的作业系统Sugar项目的大多数软件都是使用Python编写。

声明:

 1  从这里开始,下面及以后所有的程序都在python3 的环境中编写并运行,不考虑兼容2.x 版本的python

 2  博主假定大家已经部署完python3并安装好了Pycharm 

Python 的注释:

  python 使用 # 来注释单 这点和shell 一样

             使用 ''' '''  来注释多行代码

Python 的变量名称注意事项:

 1 变量的第一个字符不能为数字

 2 变量不能使用以下关键字

   

1  ['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']

Hello world:

1 print("Hello World!")

输入:

name = input("your nanme: ")
print("hello ",name)

基本判断:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC

ture_age = 30

guess_age = int(input("you guess the age si:"))

if guess_age == ture_age:
    print("ok")
elif guess_age > ture_age:
    print("smaller...")
else:
    print("bigger...")

判断:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC

import getpass

_username = "dcc"
_password = "abc123"
username = input("username:")
#password = input("password:")
password = getpass.getpass("password:")

#print(username,password)

if _username == username and _password == password:
    #print("{name} ok".format(name=username))
    print("%s ok!" % (username))
else:
    print("error")
print(username,password)

多种结果的判断:

valid_name = "dcc1"
valid_name2 = "dcc2"
input_name = input("your nanme: ")
print("hello ",input_name)
if input_name == valid_name:
    print("welcome {_valid_name} to your server".format(_valid_name=valid_name))
elif input_name == valid_name2:
    print("welcome {_valid_name2} to your server".format(_valid_name2=valid_name2))
else:
    print("Invalid name")

while循环计数:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC
ture_age = 30
count = 0
while count < 3:
    guess_age = int(input("you guess age:"))
    if guess_age == ture_age:
        print("ok")
        break
    elif guess_age > ture_age:
        print("smaller...")
    else:
        print("bigger...")
    count +=1
else:
    print("you guess too many times...")

while 跳出循环:

valid_name = "dcc1"
valid_name2 = "dcc2"
count = 0
while count < 3:
    input_name = input("your nanme: ")
    print("hello ",input_name)
    if input_name == valid_name:
        print("welcome {_valid_name} to your server".format(_valid_name=valid_name))
        break
    elif input_name == valid_name2:
        print("welcome {_valid_name2} to your server".format(_valid_name2=valid_name2))
        break
    else:
       print("Invalid name")
    count += 1  # count = count + 1

while循环重置:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC
ture_age = 30
count = 0
while count < 3:
    guess_age = int(input("you guess age:"))
    if guess_age == ture_age:
        print("ok")
        break
    elif guess_age > ture_age:
        print("smaller...")
    else:
        print("bigger...")
    count +=1
    if count == 3:
        countine_confirm = input("do you want to keep guess..?")
        if countine_confirm != 'n':
            count = 0

跳出本次循环continue:

valid_name = "dcc1"
valid_name2 = "dcc2"
for i in range(3):
    input_name = input("your nanme: ")
    print("hello ", input_name)
    if input_name == valid_name:
        print("welcome {_valid_name} to your server".format(_valid_name=valid_name))
        continue
    elif input_name == valid_name2:
        ("welcome {_valid_name2} to your server".format(_valid_name2=valid_name2))
        continue
else:
    print("Invalid name")

跳出多层循环:

python 不可以跳出多级循环,因此需要加一个计数器,在子循环中来改变这个计数器达到父循环跳出的目的

复制代码
 1 count = 0
 2 for i in range(10):
 3     print("this is i___{_i} ".format(_i=i))
 4     for b in range(10):
 5         print("this is b____{_b} ".format(_b=b))
 6         if b == 6:
 7             count = 1
 8             break
 9     if count == 1:
10         break
复制代码

模块:

  python 的模块类似于shell 中的一个函数,可以导入使用,python 官方自带了很多库,当然第三方库更丰富一些。

  使用官方的密码库来让密码输入不可见:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC

import getpass

_username = "dcc"
_password = "abc123"
username = input("username:")
#password = input("password:")
password = getpass.getpass("password:")

#print(username,password)

if _username == username and _password == password:
    #print("{name} ok".format(name=username))
    print("%s ok!" % (username))
else:
    print("error")
print(username,password)

for循环嵌套:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC
for i in range(10):
    print("-----------",i)
    for j in range(10):
        print(j)
        if j>3:
            break

四种格式打印(''''''):

#!/usr/bin/env python
#-*- coding:utf-8 -*-
print(1)
print("Hello World!")

name = "Hello World!"
print("My name is",name)

print(name)
print("name")

#interaction
username = input("username:")
age = input("age:")
#age = int(input("age:"))
#print(type(age))
job = input("Job;")
salary = input("salary:")
#拼接

info1 = '''
    ------------------ info1 of ''' + username + '''------------------
    Name:''' + username + '''
    Age:''' + age + '''
    Job:''' + job + '''
    Salary:''' + salary
print(info1)

#%s
info2 = '''
    ------------------info2 of %s -----------------
    Name;%s
    Age:%s
    Job:%s
    Salary:%s
''' % (name,username,age,job,salary)
print(info2)

#赋值
info3 = '''
------------------info3 of {_name} -----------------
    Name;{_name}
    Age:{_age}
    Job:{_job}
    Salary:{_salary}
    '''.format(_name=name,
                _age=age,
                _job=job,
                _salary=salary)
print(info3)

info4 =  '''
    ------------------info4 of {0} -----------------
    Name;{0}
    Age:{1}
    Job:{2}
    Salary:{3}
    '''.format(username,age,job,salary)

print(info4)
原文地址:https://www.cnblogs.com/dcc001/p/5705507.html