The basic operation of the python (note, input and output variables, data type)

A. Notes

不论在python2.7还是python3.6版本中,单行注释都是“#”,多行注释是“ “”" ”

Here Insert Picture Description
note:

Take the time to write python vim program:
execute the content using python3.6 python2.7 version version, you will find there has been an error, indicating that the two versions of the format is not universal:
the default input 1. When using a python 2.7 Python
2.python 'Hello World' is python2.7 version format, python ( 'hello world') is the version of the format python3.6
3.pyhton3.6 default usage utf-8, python2.7 specify code # Coding: UTF-8
4. best when writing the file name ends with .py, it would be more standardized
5.python2.7 some formats can be used python3.6 version, but in turn can not.

Two: python O

python2.7 inside:

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

Here Insert Picture DescriptionHere Insert Picture DescriptionFormatted output

String% s
% d shaping
% f float

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

Here Insert Picture Description

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

Here Insert Picture Description

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

Here Insert Picture Description

In [22]: scale = 0.1                                                    

In [23]: print('数据比例是 %.2f' %(scale * 100))                        
数据比例是 10.00

In [24]: print('数据比例是 %.2f%%' %(scale * 100))   
数据比例是 10.00%

Here Insert Picture Description

III. Variables

Variable naming rules:

Hump command method:
1. Big Hump: The first letter of each word is capitalized LastName FirstName
2. small hump: the first word beginning with a lowercase letter, the first letter of subsequent words capitalized 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

Here Insert Picture DescriptionHere Insert Picture Description

IV: Data Types

python3.6

>>> a = 13
>>> type(a)
<class 'int'>
>>> a = 1245123512512561251245124124124
>>> 
>>> type(a)
<class 'int'>

python2.7

>>> a = 13
>>> a
13
>>> type(a)
<type 'int'>
>>> a = 1234124512512341235124512512
>>> type(a)
<type 'long'>

Here Insert Picture Description

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

Here Insert Picture Description

Delete variables in memory

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

Here Insert Picture DescriptionBoolean data type (not empty and 0 are true)

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

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/bmengmeng/article/details/93974195