python学习第一篇

######python的源码编译安装######

#安装平台
-Linux
    -官网下在源码安装包(python3.6)
    -解压安装包
    -安装编译过程中需要的依赖包:gcc,zlib,zlib-devel,openssl-devel,readline,readline-devel
    -进入解压的安装包进行编译

    cd /opt/Python3-*/
    ./configure --prefix=/usr/local/python3.6 --with-ssl

    make && make install
    -添加python3命令到系统环境变量里

#方法1:
    echo $PATH
    #临时添加
    export PATH='python3命令所在的路径:$PATH'
    #永久添加
    echo export PATH='python3命令所在的路径:$PATH' >> ~/.bashrc
    #重新读取配置文件:
    source ~/.bashrc

#方法2:
    做软链接

##测试是否安装成功?
    python3.6

第一个python程序:

print('hello world')

[kiosk@foundation8 bin]$ ./python3
bash: ./python3: Permission denied
[kiosk@foundation8 bin]$ ./python3.6
Python 3.6.6 (default, Jan 11 2019, 20:41:06) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world

python语言是没有分号,严格按照缩进的语言

注释:

主要用于pycharm中,后面会学习用pycharm写python程序

单行注释以#开头,例如:
print('hello world')   # 打印hello world
多行注释:
"""
注释内容
"""

python3的输入:

num=input("请输入:")

[root@foundation15 bin]# ./python3
Python 3.6.6 (default, Jan 11 2019, 20:41:06) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num=input("请输入:")
请输入:3
>>> print(num)
3

python2的输入:

[root@foundation15 bin]# python
Python 2.7.5 (default, Aug  2 2016, 04:20:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> num=getpass.getpass("请输入:")
请输入:
>>> 21
21

输入的数据类型:

>>> age=getpass.getpass("请输入年龄:")
请输入年龄:
>>> 18
18
>>> type(age)
<type 'str'>           #可以看出,这是一个字符串型的数据
>>> int(age)           #将字符串型数据强制转换成整形数据
18
>>> type(int(age))
<type 'int'>

整型数据只能与整型数据比较:

>>> age > 19         #此处的age为字符串型
True
>>> int(age) > 19    当转换成整型时,才会成功比较
False

格式化输出:

%s   字符串
%d   整型
%f   浮点数

[root@foundation15 bin]# ./python3
Python 3.6.6 (default, Jan 11 2019, 20:41:06) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name = 'hellow'
>>> age = 11
>>> print("姓名:%s,年龄:%d" %(name,age))
姓名:hellow,年龄:11
>>> num = 3.1415926
>>> print("%.2f" %num)
3.14
>>> print("%.3f" %num)
3.142
>>> print("%.4f" %num)
3.1416

变量:

 驼峰命令法:
#     1.大驼峰:每一个单词的首字母都大写
#     FirstName LastName
#     2.小驼峰:第一个单词以小写字母开始,后续单词的首字母大写
#     firstName lastName

变量名只有在第一次出现的时候才是定义变量

>>> name = 'hello'
>>> name
'hello'
>>> type(name)
<class 'str'>        #类型字符串 
>>> num = 123
>>> num
123
>>> type(num)
<class 'int'>         #类型为整型
>>> select = True
>>> select
True
>>> type(select)
<class 'bool'>        #类型为布尔型
>>> height = 3.14
>>> height
3.14  
>>> type(height)      #类型为浮点数型
<class 'float'>

小练习:求平均成绩

"""
- 输入学生姓名
- 依次输入学生的三门科目成绩
- 计算该学生的平均成绩,并打印
- 平均成绩保留一位小数
- 计算语文成绩占总成绩的百分比,并打印
"""

[root@foundation15 day01]# /usr/local/python3.6/bin/python3 求平均成绩.py 
请输入学生姓名:xiaohong
请输入该学生的语文成绩:83
请输入该学生的数学成绩:91
请输入该学生的英语成绩:78
学生xiaohong的平均成绩为84.0
学生xiaohong的语文成绩占总成绩的32.94%
[root@foundation15 day01]# cat 求平均成绩.py 
Name = input("请输入学生姓名:")
Chinese = input("请输入该学生的语文成绩:")
Math = input("请输入该学生的数学成绩:")
English = input("请输入该学生的英语成绩:")
Sumscore = float(Chinese) + float(Math) + float(English)
Avgscore = Sumscore / 3
print("学生%s的平均成绩为%.1f" %(Name,Avgscore))
ChinesePercent = float(Chinese) / Sumscore
print("学生%s的语文成绩占总成绩的%.2f%%" %(Name,ChinesePercent*100))

数据类型:

数据类型:
#python2
>>> a = 13
>>> a
13
>>> type(a)
<type 'int'>      #a为整型
>>> a = 1234124512512341235124512512
>>> type(a)
<type 'long'>     #a为长整型


#python3
>>> a = 13
>>> type(a)
<class 'int'>    #a为整型
>>> a = 1245123512512561251245124124124
>>> 
>>> type(a)
<class 'int'>    #python3中不分整型和长整型,统一为整型

>>> a = 1
>>> float(a)
1.0
>>> b = 2.3
>>> int(b)       #将float类型的数据强制转换为int型
2
>>> float(b)
2.3
>>> str(b)       #将float类型的数据强制转换为str型
'2.3'
>>> str = 'westos'
>>> float(str)   #不能将非数字的字符串型数据转换为float或者int类型
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'westos'

#删除内存中的变量

>>> 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

#因为a的内存已被删除,因此回报错,报错原因是a没有定义

#布尔数据类型

>>> a = 'hello'
>>> bool(a)
True
>>> bool(0)
False
>>> b = ''
>>> b
''
>>> bool(b)
False

猜你喜欢

转载自blog.csdn.net/lm236236/article/details/86547101