900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python输入数据类型_python基础(py3.6安装 注释 输入输出 变量 数据类型 算术运算符号)...

python输入数据类型_python基础(py3.6安装 注释 输入输出 变量 数据类型 算术运算符号)...

时间:2018-11-04 07:13:43

相关推荐

python输入数据类型_python基础(py3.6安装 注释 输入输出 变量 数据类型 算术运算符号)...

python安装

python3:

1.拿到源码包(3.6) —>去官网下载()

2.tar zxf Python-3.6.4.tgz -C /opt/ #解压安装包

3.进入解压目录编译和安装:

yum install gcc zlib zlib-devel openssl-devel -y #解决依赖性

4.cd /opt/ cd Python-3.6.4/ #进入解压的安装包进行编译

./configure --prefix=/usr/local/python3 --with-ssl

–prefix:安装路径 --with-ssl:添加ssl加密

make && make install:安装

5.测试:cd /usr/local/python3/bin ./python3

6.添加python3的命令到环境变量中

方法1:

临时添加:

export PATH="/usr/local/python3/bin:$PATH"

python3

Python 3.6.4 (default, Aug 26 , 21:44:00)

[GCC 4.8.2 0120 (Red Hat 4.8.2-16)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

永久添加:

echo export PATH="/usr/local/python3/bin:$PATH" >> ~/.bashrc

vim ~/.bashrc

source ~/.bashrc

python3

Python 3.6.4 (default, Aug 26 , 21:44:00)

[GCC 4.8.2 0120 (Red Hat 4.8.2-16)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> quit

Use quit() or Ctrl-D (i.e. EOF) to exit

>>>

方法2:

做软连接

ln -s /usr/local/python3.6/bin/python3.6 /usr/local/bin/

##测试是否安装成功

python3

##安装ipython

cd /usr/local/python3.6/bin

pip3.6 install ipython

第一个py命令

#coding:utf-8

#python2.x:默认使用ASCII编码

#python3.x:默认使用UTF-8编码

#1.没有分号(编码规范 PEP8)

#2.严格按照缩进的语言

print('白茶清欢无别事')

print('越努力')

##注释

#单行注释

“”"

xxxxx 多行注释

xxxx

“”"

输入和输出

##python3.x

input():接收任意数据类型

python3.x中没有raw_input()

>>> input('Num:')

Num:2

'2'

>>> input('Num:')

Num:abc

'abc'

>>> input('Passwd:')

Passwd:123

'123'

#输入内容不回显

>>> import getpass

>>> num = getpass.getpass('请输入密码:')

请输入密码:

>>> print(num)

123

##python2.x

input():只支持正确的数值类型

raw_input():数值和字符串

>>> input('Num:')

Num:2

2

>>> input('Num:')

Num:1.2

1.2

>>> input('Num:')

Num:redhat

Traceback (most recent call last):

File "", line 1, in

File "", line 1, in

NameError: name 'redhat' is not defined

>>> raw_input('Num:')

Num:2

'2'

>>> raw_input('Num:')

Num:1,2

'1,2'

>>> raw_input('Num:')

Num:redhat

'redhat'

>>>

#如果接收到的数值要进行比较的时候,一定要转换为同一种类型

>>> age = input('age:')

age:19

>>> age

'19'

>>> age > 18

Traceback (most recent call last):

File "", line 1, in

TypeError: '>' not supported between instances of 'str' and 'int'

>>> age = int(age)

>>> age

19

>>> age > 18

True

>>> age = int(input('age:'))

age:18

>>> age

18

格式化输出

格式化输出

%s:代表字符串 %d:整型

>>> name = 'westos'

>>> name

'westos'

>>> age = 12

>>> print('%s的年龄是%d' %(name,age))

westos的年龄是12

>>> age = 18

>>> print('%s的年龄是%d' %(name,age))

westos的年龄是18

>>> age = '19'

>>> print('%s的年龄是%d' %(name,age))

Traceback (most recent call last):

File "", line 1, in

TypeError: %d format: a number is required, not str

>>> age = 19.5

>>> print('%s的年龄是%d' %(name,age))

westos的年龄是19

浮点型 %f

>>> money=23121312.32314432

>>> name = 'Tom'

>>> print('%s的工资为%f' %(name,money))

Tom的工资为23121312.323144

>>> money=60000

>>> print('%s的工资为%f' %(name,money))

Tom的工资为60000.000000

>>> money=60000.99

>>> print('%s的工资为%f' %(name,money))

Tom的工资为60000.990000

>>> print('%s的工资为%.2f' %(name,money))

Tom的工资为60000.99

>>> money=60000

>>> print('%s的工资为%.3f' %(name,money))

Tom的工资为60000.000

整数的占位:不够的位数 前面补0

>>> sid = 1

>>> name = 'lily'

>>> print('%s的学号为000%d' %(name,sid))

lily的学号为0001

>>> sid = 2

>>> print('%s的学号为000%d' %(name,sid))

lily的学号为0002

>>> sid = 10

>>> print('%s的学号为000%d' %(name,sid))

lily的学号为00010

>>> print('%s的学号为%.5d' %(name,sid))

lily的学号为00010

>>> sid = 1

>>> print('%s的学号为%.5d' %(name,sid))

lily的学号为00001

>>> sid = 20

>>> sid = 100

>>> print('%s的学号为%.5d' %(name,sid))

lily的学号为00100

百分号的实现

>>> scale = 0.1

>>> print('数据的比例是:%.2f' %(scale))

数据的比例是:0.10

>>> print('数据的比例是:%.2f' %(scale * 100))

数据的比例是:10.00

>>> print('数据的比例是:%.2f%' %(scale * 100))

Traceback (most recent call last):

File "", line 1, in

ValueError: incomplete format

>>> print('数据的比例是:%.2f%%' %(scale * 100))

数据的比例是:10.00%

变量

#驼峰命令法:

1.大驼峰:每一个单词的首字母都大写

FistName LastName

2.小驼峰:第一个单词小写开始后续单词首字母大写

fistName lastName

#str:表示一个字符串类型

name = '房东的喵'

print(name)

#int:表示一个整型

age = 22

print(age)

#float:表示一个浮点型

height = 178.5

print(height)

#bool:表示一个布尔型,只有True和False两个值

gender = True

print(gender)

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

数据类型

整型

>>> a = 1

>>> print(a)

1

查看变量的类型

>>> type(a)

浮点型

>>> b = 1.2

>>> print(b)

1.2

>>> type(b)

>>> c = westos

Traceback (most recent call last):

File "", line 1, in

NameError: name 'westos' is not defined

字符串型

>>> c = 'westos'

>>> print(c)

westos

>>> c = "what's"

>>> print(c)

what's

>>> c = 'what's'

File "", line 1

c = 'what's'

^

SyntaxError: invalid syntax

>>> c = 'what\'s'

>>> print(c)

what's

bool型(只有两个值:True False 非0即真)

>>> a = 1

>>> bool(a)

True

>>> bool(0)

False

>>> bool('')

False

>>> bool(' ')

True

>>> bool('redhat')

True

数据类型之间的转换

>>> a = 1

>>> type(a)

>>> float(a)

1.0

>>> type(a)

>>> b = float(a)

>>> b

1.0

>>> b = 2.0

>>> int(b)

2

>>> c = 'redhat'

>>> int(c)

Traceback (most recent call last):

File "", line 1, in

ValueError: invalid literal for int() with base 10: 'redhat'

>>> b = 123

>>> str(b)

'123'

>>> c = '123'

>>> int(c)

123

>>> a

1

>>> b

123

>>> c

'123'

在内存中删除一个变量

>>> del a

>>> a

Traceback (most recent call last):

File "", line 1, in

NameError: name 'a' is not defined

>>> del b

>>> b

Traceback (most recent call last):

File "", line 1, in

NameError: name 'b' is not defined

算术运算符号

python2.x

>>> 5/2

2

>>> 100/300

0

>>> 5.0/2

2.5

>>> 100/300.0

0.3333333333333333

python2.x中除法需要将其中一个写为浮点型,不然回自己取整

python3.x

>>> 5/2

2.5

>>> 100/300

0.3333333333333333

>>>

取余

>>> 5%2

1

取整

>>> 5//2

2

>>> a = 1

>>> a = a+1

>>> a

2

>>> a += 1

>>> a

3

逻辑运算符

“”"

and

1 and 2

两个条件同时满足 就返回True

只要有一个条件不满足,就返回False

or

1 or 2

两个条件只要满足一个 就返回True

两个条件都不满足,就返回False

“”"

python_score = 60

c_score = 50

if python_score >= 60 and c_score >=60:

print('考试通过')

else:

print('考试不通过')

标签:name,python,age,数据类型,py3.6,Num,print,input,python3

来源: /luxunlx123/article/details/100228104

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。