首页 > 代码库 > Python 第一周学习笔记
Python 第一周学习笔记
1、Python 解释器
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
Python3 字符集默认支持中文
2、变量定义的规则:
。变量名只能是字母、数字或下划线的任意组合
。变量名的第一个字符不能是数字
3、字符串
所有带引号的都是字符串,包含(单引号,双引号,三引号)
4、注释
当行注释:#被注释内容
多行注释:"""被注释内容""" (可以是单引号或者是双引号)
规范:开发写代码每一行最多不应该超过80个字符
以后写代码 要经常写注释,方便以后查看代码
5、格式化字符串
输入姓名、年龄、工作
%s 表示格式化字符串 %d表示格式化数字 %f表示浮点数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
name = input("input your name:")
age = int(input("input your age:") ) #int 的作用是把input输入的字符串改变为数字
job = input("input your job:")
msg=‘‘‘
Infomation of user %s
------------------
Name: %s
Age: %d
Job: %s
-------End--------
‘‘‘% (name,name,age,job)
print(msg)
6、常用模块初始
Python3 插入模块 (插入import 密文模块 getpass) import是插入一个模块
新手学习python,经常要使用python命令行查找一些不熟悉的使用方法等等,但是python命令行下没有自带tab补全的功能,着实让我这新手菜了....不过这好在是个互联网的时代,没有多大的事,互联一下,特此记录。
vi tab.py
#!/usr/bin/env python
# python startup file
import sys
import readline
import rlcompleter
import atexit import os
# tab completion
readline.parse_and_bind(‘tab: complete‘)
# history file histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
代码有了,我们还需要将脚本放到python指定的目录下,可以使用sys.path来查看一下
>>> import sys >>> sys.path [‘‘, ‘/usr/lib/python2.7‘, ‘/usr/lib/python2.7/plat-x86_64-linux-gnu‘, ‘/usr/lib/python2.7/lib-tk‘, ‘/usr/lib/python2.7/lib-old‘, ‘/usr/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages/PILcompat‘, ‘/usr/lib/python2.7/dist-packages/gtk-2.0‘, ‘/usr/lib/python2.7/dist-packages/ubuntu-sso-client‘]
一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下
打印并保存
>>> import os
>>> cmd_res = os.popen("df -h").read() #使用os.popen把命令保存在内存里,然后用read读取出来
>>> print(cmd_res)
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.5G 16G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 52M 129M 29% /boot
7、基本的if判断及if else语句
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
user = ‘zzg‘
passwd = ‘centos‘
username = input("username:")
password = input("password:")
if user == username:
print("username is correct...")
if passwd == password:
print("welcome login...")
else:
print("password is invalid...")
else:
print("用户名输入错误。。。")
优化:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
user = ‘zzg‘
passwd = ‘centos‘
username = input("username:")
password = input("password:")
if user == username and passwd == password:
print("welcome login...")
else:
print("用户名与密码错误。。。")
脚本:
用if写个猜年龄的脚本,提示:猜小了,提示往大了猜,猜大了,往小的猜。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
age = 30
guess_num = int( input("input guess your num:") )
if guess_num == age:
print("恭喜你,猜对了!")
elif guess_num > age:
print("请往小的猜。。。")
else:
print("请往大的猜。。。")
脚本:
猜年龄游戏for循环
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
age = 30
for i in range(10):
guess_num = int( input("input guess your num:") )
if guess_num == age:
print("恭喜你,猜对了!")
break #不往后循环了,跳出整个循环
elif guess_num > age:
print("请往小的猜。。。")
else:
print("请往大的猜。。。")
优化1:
猜年龄游戏,最多只允许猜3次
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
age = 30
for i in range(10):
if i < 3:
guess_num = int( input("input guess your num:") )
if guess_num == age:
print("恭喜你,猜对了!")
break #不往后循环了,跳出整个循环
elif guess_num > age:
print("请往小的猜。。。")
else:
print("请往大的猜。。。")
else:
print("请求太多,稍后再试。。。")
break
8、循环语句
猜年龄游戏,每猜3次提示是否继续,直到第10次退出
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Tian Ba
age = 30
counter = 0
for i in range(10):
print(‘-->counter:‘,counter)
if counter < 3:
guess_num = int( input("input guess your num:") )
if guess_num == age:
print("恭喜你,猜对了!")
break #不往后循环了,跳出整个循环
elif guess_num > age:
print("请往小的猜。。。")
else:
print("请往大的猜。。。")
else:
continue_confirm = input("请问你是否还要继续:")
if continue_confirm == ‘y‘:
counter = 0
continue #跳出当前循环,重新开始循环
else:
print("结束")
break
counter += 1 #counter = counter + 1
本文出自 “liunx” 博客,谢绝转载!
Python 第一周学习笔记