Python基础(pytho3.6安装,注释,输入输出,变量,数据类型)

1.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 KaTeX parse error: Expected 'EOF', got '#' at position 7: PATH #̲临时添加 export PA…PATH’
#永久添加
echo export PATH=‘python3命令所在的路径:$PATH’ >> ~/.bashrc
#重新读取配置文件:
source ~/.bashrc

#方法2:
做软链接
在这里插入图片描述
##测试是否安装成功?
python3.6
##安装ipython
在这里插入图片描述
在这里插入图片描述

第一个python命令

#coding:utf-8
#1.没有分号
#2.严格按照缩进的语言

print(‘hello world’)

注释

#coding:utf-8

#这是一个单行注释
print(‘hello world’) #注释2

“”"
这是一个
多行注释
“”"
print(‘hello westos’)

输入输出

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

##格式化输出

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

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’

n [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 [25]: print(‘数据比例是 %.2f%%’ %(scale * 100))
数据比例是 10.00%

变量

驼峰命令法:

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

Out[14]: 58.75

数据类型

#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'>                                                                                 ###在python3中默认为int,没有long

>>> a = 'hello'
>>> a
'hello'
>>> a.center(40)
'                 hello                  '
>>> a.center(40,'*')
'*****************hello******************'
>>> print("学生管理系统".center(50,'-'))
----------------------学生管理系统----------------------
>>> print("学生管理系统".center(50,'*'))
**********************学生管理系统**********************



>>> a = 1
>>> float(a)                             ####  float浮点型
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'

#删除内存中的变量
>>> 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 = 'hello'
>>> bool(a)
True
>>> bool(0)
False
>>> b = ''
>>> b
''
>>> bool(b)
False

猜你喜欢

转载自blog.csdn.net/weixin_43407305/article/details/86518847