标签:
目录
1 Python介绍... 1
1.1 目前Python主要应用领域:... 1
2 python安装... 1
2.1 windows安装... 1
2.2 linux安装... 2
3 python基础实战... 3
3.1 PyCharm用到的快捷键... 3
3.2 打印hello world. 3
3.3 python解释器... 3
3.4 字符编码... 4
3.5 变量定义规则... 4
3.5.1 定义变量... 4
3.5.2 变量赋值... 4
3.6 行注释... 5
3.7 用户输入... 5
3.7.1 输入密码不可见... 6
python的创始人为吉多·范罗苏姆(Guido van Rossum)。1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承。
下载安装包www.python.org
安装到c英文路径
配置环境变量:
计算机---属性---高级系统设置---高级---环境变量---Path[注意这里是用;分号分割的]
搜狐下载http://mirrors.sohu.com/python/3.5.1/ 官网慢的一坨
wget http://mirrors.sohu.com/python/3.5.1/Python-3.5.1.tgz
tar xf Python-3.5.1.tgz
cd Python-3.5.1
./configure
make && make install
安装完成,默认路劲
/usr/local/bin/python3 –V
#可以更改系统默认python版本,最好做个软连接到/usr/bin/ 方便指定解释器
ln -s /usr/local/bin/python3 /usr/bin/
mv /usr/bin/python /usr/bin/python2.bak
ln -s /usr/local/bin/python3 /usr/bin/python
Ctrl+d复制一行内容
Ctrl+?批量注释多行
PyCharm:
print("hello world")
windows:
C:\Users\lenovo>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> print ("hello world")
hello world
也可以编辑.py脚本拖到windows终端
C:\Users\lenovo>python "C:\Users\lenovo\Desktop\new 1.py"
linux:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
print ("hello world")
print ("你好,世界")
可以直接指定python路径
#/usr/bin/python
指定版本,这只是在chmod +x后给脚本执行权限后生效,默认python test.py只执行系统默认的python版本。
#/usr/bin/env python
#/usr/bin/env python3
python2.x默认只支持ASCII码,中文需要声明字符集
#!/usr/bin/env python
#-*- coding:utf-8 -*-
变量名只能是 字母、数字或下划线的任意组合
变量名的第一个字符不能是数字
以下关键字不能声明为变量名
字符创定义要加“”双引号
[‘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‘]
day1 = 1000 + 500 + 500
day2 = 3000
day3 = 1000
day4 = 4000
total = day1 + day2 + day3 + day4
print("总和:",total)
总和: 10000
name = "zhang"
name2 = name
print (name,name2)
zhang zhang
单行注释用: #
多行注释:
"""
三个双引号注释多行内容
三个双引号注释多行内容
"""
‘’’
三个双引号注释多行内容
三个双引号注释多行内容
‘’’
批量注释:
鼠标选中 Ctrl+? 实现批量注释
例2:
name = input("What is your name?")
print ("hello",name)
结果:
What is your name?sdfas
hello sdfas
例2:\n换行
name = input("What is your name?")
# print ("hello",name)
job = input("job: ")
hobby = input("hobby: ")
print("my name is",name,"\nmy job is",job,"\nmy hobby is",hobby)
结果:
What is your name?zh
job: it
hobby: it
my name is zh
my job is it
my hobby is lol
例3:
测试值
str = "%s"%"hello"
num = "%d"%33
print(str)
print(num)
name = input("What is your name?")
# print ("hello",name)
age = input("age:")
job = input("job: ")
hobby = input("hobby: ")
print("my name is",name,"\nmy job is",job,"\nmy hobby is",hobby)
info = ‘‘‘
--------info of %s--------
name: %s
age : %s
jog : %s
hobby: %s
--------end---------
‘‘‘%(name,name,age,job,hobby)
print (info)
结果:其中第一个name是标题
What is your name?zh
age:22
job: it
hobby: lol
my name is zh
my job is it
my hobby is lol
--------info of zh--------
name: zh
age : 22
jog : it
hobby: lol
--------end---------
%d整数问题
age = int(input("年龄:"))
info = ‘‘‘
--------info---------
age : %d
--------end--------
‘‘‘%(age)
print(info)
结果:
年龄:32
--------info---------
age : 32
--------end--------
代码:
name = input("input you name:")
age = int(input("input you age:"))
print(type(age),type(name))
job = input("input your job:")
salary = int(input("input your salary:"))
info = ‘‘‘
--------------informathion of %s
Name: %s
Age : %d
Job : %s
Salary: %d
--------------end----------
‘‘‘% (name,name,age,job,salary)
print(info)
结果:
input you name:zhang
input you age:25
<class ‘int‘> <class ‘str‘>
input your job:IT
input your salary:8888
--------------informathion of zhang
Name: zhang
Age : 25
Job : IT
Salary: 8888
--------------end----------
Process finished with exit code 0
条件表达式,条件行之后要接:冒号,否则语法错误
username = input("username: ")
password = input("password: ")
if username == "zhang" and password == "123456":
print("login successd")
else:print("username or password is worng")
需要定义getpass模块,定义模块只能在服务器测试
#!/usr/bin/env python
import getpass
pwd = getpass.getpass("密码:")
print (pwd)
结果:
[root@localhost ~]# python test3.py
密码:这里看不到输入的密码值
123456
oldboy_age = 39
guess_age = int ( input("age:") ) #str --> int
if guess_age == oldboy_age:
print("correct!!!")
elif guess_age > oldboy_age: #else if
print("try smaller...")
else:
print("try bigger...")
猜大
age:50
try smaller...
Process finished with exit code 0
猜小
age:20
try bigger...
Process finished with exit code 0
正确
age:39
correct!!!
Process finished with exit code 0
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定
count = 0
flag = 1
lock = []
user_pass = []
username = input(‘username:‘)
f = open(‘heimingdan.txt‘,‘r‘)
lock_file = f.readlines()
f.close()
for i in lock_file:
line = i.strip(‘\n‘)
lock.append(line)
if username in lock:
print(‘你的用户%s已经被锁定请联系管理员‘ %username)
else:
while True:
count += 1
passwd = input("password:")
f = open(‘yonghu.txt‘,‘r‘)
user_file = f.readlines()
f.close()
for i in user_file:
test = 3 - (count)
user_pass = i.strip().split()
if username == user_pass[0] and passwd == user_pass[1]:
print(‘欢迎回来 %s‘ %username)
flag = True
break
else:
print(‘密码错误请重新输入 你还有 %s 机会‘ %test)
continue
if flag is True:
break
else:
if count == 3:
print(‘你的用户%s以被锁定,请联系管理员 ‘ %username)
lock_file = open(‘heimingdan.txt‘,‘a‘)
lock_file.write(‘%s\n‘ %username)
lock_file.close()
break
错误
username:abc
password:123
密码错误请重新输入 你还有 2 机会
password:1234
密码错误请重新输入 你还有 1 机会
password:12345
密码错误请重新输入 你还有 0 机会
你的用户abc以被锁定,请联系管理员
成功
username:oldboy
password:123456
欢迎回来 oldboy
Process finished with exit code 0
标签:
原文地址:http://www.cnblogs.com/zhangzeng/p/5963050.html