1.python的介绍
1).为什么python使用这么多?
python语法简单,上手容易,精通难。现在使用爬虫比较多,还可以作前端。
例如python的交换:x,y=y,x
2)简单介绍
python的作者是Guido,归叔,1989年,圣诞节,编写出来
现在使用的大部分是python是3版本,使用python可以查看版本
知乎,豆瓣,使用python比较多
python有强大的第三方模块库
2.python3.6安装过程:
查看安装环境 编译 安装
configure make make install
步骤一:官网下在源码安装包(python3.6),并解压
tar zxf Python-3.6.6.tgz
ls
cd Python-3.6.6/
步骤二:安装编译过程中需要的依赖包:gcc,zlib,zlib-devel,openssl-devel,readline,readline-devel
yum install gcc zlib zlib-devel openssl-devel readline readline-devel -y
步骤三:进入安装包内:安装python
./configure --prefix=/usr/local/python3.6 --with-ssl ##带有ssl安装
make && make install
步骤四:加载环境变量
$PATH 显示环境变量的路径,此路径下的软件可以在任何路径使用
#方法1:
echo $PATH
#临时添加
export PATH='python3命令所在的路径:$PATH'
#永久添加
echo export PATH='python3命令所在的路径:$PATH' >> ~/.bashrc
#重新读取配置文件:
source ~/.bashrc
#方法2:
做软链接
软链接python3.6到环境变量:ln -s /usr/local/python3.6/bin/python3.6 /usr/local/bin
步骤五:复制python3.6到别的电脑:scp -r /usr/local/python3.6/root@172.25.254.38:/usr/local/
可继续在别的电脑上,制作软链接
测试是否安装成功?
python3.6
3.第一个python代码,以及一些基本内容
hello world代码的编写
#_*_coding:utf-8_*_
print('hello world')
1)基本内容
一.python没有分号
二.python代码的文件名都是以py结尾
三.python严格缩进 4个空格
四.使用方法: python 文件名.py
2)python2和3的区别
python2
不支持中文:使用时候加入编码格式 :#_*_coding:utf-8_*_
python2的输出格式:print 'hello world'
python3
支持中文
python3的输出格式:print('hello world')
3)代码的注释
单行注释
#
多行注释
"""
"""
测试:
#_*_coding:utf-8_*_
#这是一个单行注释
print('hello world') #注释2
"""
这是一个
多行注释
"""
print('hello westos')
4.接收用户输入
1)用户的普通输入
python2
input('NUM') 只能输入整型
raw_input('num') 输入字符串
python3
input('num') ##只有input,将所有的数字视为字符串
2)用户的密码输入
import getpass ##加载getpass库,输入密码不显示
num=getpass.get.getpass('请输入密码:')
测试:
>>> import getpass
>>> num=getpass.getpass('请输入密码:')
请输入密码:
>>> num
'redhat'
>>>
>>>
>>>
>>> age=raw_input('请输入年龄:')
请输入年龄:18
>>> age
'18'
>>> type(age)
<type 'str'>
>>> int(age)
18
>>> age > 19
True
>>> age
'18'
>>> age=20
>>> age > 19
True
>>> age=raw_input('请输入年龄:')
请输入年龄:18
>>> age
'18'
>>> int(age) > 19
False
##python3
>>> num=input()
12
>>> num
'12'
>>>
5.格式化输出
1)字符的含义
%s 字符串
%d 整形
%f 浮点数 默认6位小数,可使用%.2f 保留两位ie小数,%2f 前占两位,用0补齐
布尔型 bool
python2有长整形,而python3只有整型
百分号使用%%
2)ipython的安装
进入到python3.6的目录中,
./pip3.6 install ipython 安装ipython
ln -s /usr/local/python3.6/bin/ipython /usr/local/bin/ 生成软链接
测试
In [1]: name = 'westos'
In [2]: age = 11
In [3]: print('%s的年龄为%d' %(name,age))
westos的年龄为11
In [4]: name = 'redhat'
In [5]: print('%s的年龄为%d' %(name,age))
redhat的年龄为11
In [6]: age = '18'
In [7]: print('%s的年龄为%d' %(name,age))
------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-0fe41942b1ea> in <module>
----> 1 print('%s的年龄为%d' %(name,age))
In [8]: money = 8576.123123
In [9]: print('%s本月的工资为%f' %(name,money))
redhat本月的工资为8576.123123
In [10]: money = 7000
In [11]: print('%s本月的工资为%f' %(name,money))
redhat本月的工资为7000.000000
In [12]: print('%s本月的工资为%.2f' %(name,money))
redhat本月的工资为7000.00
In [13]: print('%s本月的工资为%.3f' %(name,money))
redhat本月的工资为7000.000
In [14]: print('%s本月的工资为%.1f' %(name,money))
redhat本月的工资为7000.0
In [15]: sid = 1
In [16]: print('%s的学号为%d' %(name,sid))
redhat的学号为1
In [17]: print('%s的学号为130%d' %(name,sid))
redhat的学号为1301
In [18]: print('%s的学号为111%d' %(name,sid))
redhat的学号为1111
In [19]: print('%s的学号为130%d' %(name,sid))
redhat的学号为1301
In [20]: print('%s的学号为130%.3d' %(name,sid))
redhat的学号为130001
In [21]: print('%s的学号为130%.5d' %(name,sid))
redhat的学号为13000001
In [22]: scale = 0.1
In [23]: print('数据比例是 %.2f' %(scale * 100))
数据比例是 10.00
In [24]: print('数据比例是 %.2f%' %(scale * 100))
------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-f33b74b51d9b> in <module>
----> 1 print('数据比例是 %.2f%' %(scale * 100))
ValueError: incomplete format
In [25]: print('数据比例是 %.2f%%' %(scale * 100))
数据比例是 10.00%
6.变量命名规则
1)驼峰命名规则
# 1.大驼峰:每一个单词的首字母都大写
# FirstName LastName
# 2.小驼峰:第一个单词以小写字母开始,后续单词的首字母大写
# firstName lastName
测试
# #str:表示一个字符串类型
# In [1]: name = '彭于晏'
#
# In [2]: name
# Out[2]: '彭于晏'
#
# int:表示一个整形
# In [3]: age = 18
#
# In [4]: age
# Out[4]: 18
#
# #bool:表示一个布尔型,真:True 假:False
# In [5]: gender = True
#
# In [6]: gender
# Out[6]: True
#
# #float:表示一个浮点型
# In [7]: height = 180.5
#
# In [8]: height
# Out[8]: 180.5
#
# In [9]: price = 8.5
#
# In [10]: weight = 7.5
#
# In [11]: money = price * weight
#
# In [12]: money
# Out[12]: 63.75
#
# #变量名只有在第一次出现的时候,才是定义变量
# In [13]: money = money - 5
#
# In [14]: money
练习:求平均成绩。
输入学生姓名
依次输入学生三门科目成绩
计算该学生的平均成绩,并打印
平均成绩保留一位小数
计算语文成绩占总成绩的百分比,并打印
name = input("学生姓名:")
Chinese = float(input("语文成绩:"))
Math = float(input("数学成绩:"))
English = float(input("英语成绩:"))
# 总成绩
SumScore = Chinese + Math + English
# 平均成绩
AvgScore = SumScore / 3
ChinesePercent = (Chinese / SumScore) * 100
print('%s 的平均成绩为%.1f' % (name, AvgScore))
print('语文成绩占总成绩的%.2f%%' % ChinesePercent)
7.数据类型
1)查看数据类型的方法
type(变量)
测试
#python2
>>> a = 13
>>> a
13
>>> type(a)
<type 'int'>
>>> a = 1234124512512341235124512512
>>> type(a)
<type 'long'>
#python3
>>> a = 13
>>> type(a)
<class 'int'>
>>> a = 1245123512512561251245124124124
>>>
>>> type(a)
<class 'int'>
2)字符串的方法
字符串输出的中间
a= 'hello'
a.center(40)
a.center(40,'*')
a.center(40,'*')
测试
>>> a = 'hello'
>>> a
'hello'
>>> a.center(40)
' hello '
>>> a.center(40,'*')
'*****************hello******************'
>>> print("学生管理系统".center(50,'-'))
----------------------学生管理系统----------------------
>>> print("学生管理系统".center(50,'*'))
**********************学生管理系统**********************
3)数据类型转换
转换类型 (变量)
测试
>>> a = 1
>>> float(a)
1.0
>>> b = 2.3
>>> int(b)
2
>>> float(b)
2.3
>>> str(b)
'2.3'
>>> str = 'westos'
>>> float(str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'westos'
4)删除变量
del 变量
测试
#删除内存中的变量
>>> a = 1.2
>>> a
1.2
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
5)布尔类型
空和0为 false
其他为 True
测试
>>> a = 'hello'
>>> bool(a)
True
>>> bool(0)
False
>>> b = ''
>>> b
''
>>> bool(b)
False
8.编译环境
1)pycharm的安装
步骤一.下载安装包
步骤二.解压安装包
tar zxf /home/kiosk/Desktop/pycharm-community-2018.2.2.tar.gz -C /usr/local/
cd /usr/local/
154 ls
155 mv pycharm-community-2018.2.2 pycharm ##改名字
156 ls
步骤三.安装:进入目录执行 pycharm.sh
cd /pycharm/bin
./pycharm.sh
2)python的界面的认识
一、修改python解释器的位置
先删除原来解释器
File | Settings | Project: westos | Project Interpreter
show all
添加,删除编译器
二、设置字号
File | Settings | Editor | Font
三、设置文件开始初始设置
File | Settings | Editor | File and Code Templates
选择 python script 编辑:
"""
# _*_ coding:utf-8 _*_
Name:08_pycharm的使用.py
Date:19-1-12
Author:westos-wsp
Connect:wsp439@sina.com
Desc:
"""
四、一些快捷键
- 如何快速创建文件(alt + insert)
- 格式化python代码,使风格好看(crtl + alt + l)
- 如何撤销代码的修改(ctrl + z)
- 如何取消撤销的代码(ctrl + shift + z)
- 快速重命名(shift + f6)
- 快速注释代码(ctrl + /)
- 快速取消注释代码(ctrl + /)
- 快速删除一行代码(ctrl + x)
- 快速复制一行代码(ctrl + d)
9.if语句的使用
1)if语句
if 要判断的条件:
条件成立的时候,要做的事
...
测试
#1.定义一个整数变量
# age = 18
age = 12
#2.判断是否满18岁
if age >= 18:
print('欢迎来酒吧')
print('~~~~~~~~~~')
2)if-else语句
if 要判断的条件:
条件成立的时候,要做的事
...
else :
条件不成立时,要作的事情
...
测试
#1.定义一个整数变量
# age = 12
age = 19
#2.判断是否满18岁
if age >= 18:
print('你已经成年,欢迎来酒吧')
else:
print('未成年,回家写作业')
3)if的逻辑运算符号
一、and逻辑符
条件1 and 条件2
两个条件同时满足,就返回True
两个条件有一个不满足,就返回False
测试
if age >= 0 and age <= 120:
print('正确')
else:
print('错误')
二、or逻辑符
条件1 or 条件2
两个条件只要有一个满足,就返回True
两个条件都不满足,返回False
测试
python_score = 20
c_score = 30
c_score = 70
if python_score > 60 or c_score > 60:
print('考试通过')
else:
print('准备补考')
4)if-elif
if 要判断的条件:
条件成立时,要做的事
elif 条件2:
...
else:
条件都不成立时,要做的事
elif 和 else都必须和if联合使用,不能单独使用
测试
holiday_name = '情人节'
if holiday_name == '春节':
print('吃饺子')
elif holiday_name == '元宵节':
print('吃元宵')
elif holiday_name == '情人节':
print('买礼物')
else:
print('不过节')
5)if的嵌套
练习1:安检检测
have_ticket = True
knife_length = 21
if have_ticket:
print('车票检查通过,准备安检...')
if knife_length > 20:
print('刀长度为 %d:超出限定长度,禁止入内' %knife_length)
else:
print('刀长度为 %d:未超出限定长度,允许入内' %knife_length)
else:
print('请先买票')
6)综合练习
练习1:
石头剪刀布1.从控制台输入你要出的拳 ---石头(1)/剪刀(2)/布(3)
2.电脑随即出拳
3.比较胜负
import random ##加载随机数库
# a = random.randint(1,10) ##生成随机数
# print(a)
#1.从控制台输入要出的拳
player = int(input('请输入您要出的拳:石头1/剪刀2/布3:'))
#2.电脑出拳
computer = random.randint(1,3)
print(computer)
#3.比较胜负
if ((player == 1 and computer == 2) \
or (player == 2 and computer == 3) \
or (player == 3 and computer == 1)):
print('玩家胜利~')
elif player == computer:
print('平局')
else:
print('玩家失败~')
练习2:判断是否为闰年
用户输入年份,判断是否为闰年?
- 能被400整除的是闰年,能被4整除但是不能被100整除的是闰年
year = int(input('请输入年份:'))
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) :
print('%d是闰年' %(year))
else :
print('%d不是闰年' %(year))
cal 2020 显示日历
练习3:输入年、月,输出本月有多少天
2004 2
29天
2010 4
30天
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
if month == 2:
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print('%d年的%d月有29天' % (year,month))
else:
print('%d年的%d月有28天' % (year,month))
elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
print('%d年的%d月有31天' % (year,month))
else:
print('%d年的%d月有30天' % (year,month))
练习4:要判断用户的输入是否为空
方法一和二:
value = input('Value:')
# if value == '':
# print('请输入合法的值')
if not value:
print('请输入合法的值')
练习5:用户输入月份,打印该月份所属的季节
3,4,5春季 6,7,8夏季 9,10,11秋季 12 1 2冬季
month=int(input('请输入月份:'))
if month ==3 or month ==4 or month ==5 :
print('%s为春季' %(month))
elif month ==6 or month ==7 or month ==8 :
print('%s为夏季' %(month))
elif month ==9 or month ==10 or month ==11 :
print('%s为秋季' %(month))
elif month ==12 or month ==1 or month ==2 :
print('%s为冬季' %(month))
else:
print('请输入正确的月份')