py02-python基础

1、基础

 = 号是赋值

 == 号比较的是值

 is 比较的id,id一样,type和value肯定一样。

>>> name='nq'
>>> name1='nq1'
>>> name==name1
False
>>>

>>> count1=11
>>> count2=11
>>> id(count1),id(count2)
(502661904, 502661904)
>>> count1 is count2
True
>>>
>>>

2、 程序交互

  input:

  raw_input:

 在python3中只有input,输入的都是字符串

>>> username=input('>>>: ')
>>>: egon
>>> type(username)
<class 'str'>
>>> count=input('>>>>>>>>>>>: ')
>>>>>>>>>>>: 123
>>> type(count)
<class 'str'>

 

 在python2中有input和raw_input,raw_input输入的都是字符串

>>> passwd=raw_input('>>>>>>>>: ')
>>>>>>>>: 123
>>> type(passwd)
<type 'str'>
>>> passwd1=raw_input('>>>>>>>>: ')
>>>>>>>>: egon
>>> type(passwd1)
<type 'str'>
>>>

 

input输入的是什么类型就是什么类型

>>> user_name=input('please input your username: ')
please input your username: 'xtyang'
>>> type(user_name)
<type 'str'>
>>>
>>>
>>> level=input('please input your level: ')
please input your level: [1,2,3]
>>> type(level)
<type 'list'>
>>>

 

3、字符串转换

>>> res=input('>>>>: ')
>>>>: 456
>>> type(res)
<class 'str'>
>>> res1=int(res)
>>> type(res1)
<class 'int'>
>>> type(res)

>>> res=input('>>>: ')
>>>: 19900901
>>> type(res)
<class 'str'>
>>> res1=eval(res)
>>> type(res1)
<class 'int'>

>>> name=input('>>>>>>: ')
>>>>>>: [1,2,3,4]
>>> type(name)
<class 'str'>
>>> name1=eval(name)
>>> type(name1)
<class 'list'>
>>>
>>>
>>> new=input('>>>>>: ')
>>>>>: {'old':'123','egon':'456'}
>>> type(new)
<class 'str'>
>>> new1=eval(new)
>>> type(new1)
<class 'dict'>
>>>

eval相当于把值在本地执行了一下

>>> eval('print("ok")')
ok
>>>

 4、基本数据类型

  整型数字

>>> num=10
>>> type(num)
<class 'int'>

 

  字符串

>>> username='yangxutao'
>>> type(username)
<class 'str'>

 

  列表

>>> user_name=['alex','oldboy','zhangsan','lisi']
>>> type(user_name)
<class 'list'>
>>>

 

  字典

>>> hobbies={'username':'yangxutao','user_id':'123','age':'90'}
>>> type(hobbies)
<class 'dict'>
>>>

 

  布尔值

>>> True or False and False
True
>>> (True or False) and False
False

 

  可变类型:在id不变的前提下,value可以变,则称为可变类型,如列表、字典

  不可变类型:value一旦改变,id也改变,则称为不可变类型,如字符串、数字

5、链式赋值

>>> a=b=c=d=1
>>> a,b,c,d
(1, 1, 1, 1)
>>>

 6、变量互换

>>> a='x-y'
>>> b=1
>>> a,b
('x-y', 1)
>>> a,b=b,a
>>> a,b
(1, 'x-y')
>>>

 7、格式化输出

print('my name is','egon')
print('my name is %s my age is %d' %('hello',90))

 8、数据类型

 数字分为整型 int、长整形 long、浮点型 float、复数 complex

age=int(10)
count=float(4090.01)
print(type(age))
print(type(count))

 长整形在python3中已经废弃,在python2中使用

>>> count=1990L
>>> type(count)
<type 'long'>

 

  10进制转换

count=18
print(bin(count))   #10进制转2进制
print(oct(count))   #10进制转8进制
print(hex(count))   #10进制转16进制

 

posted @ 2017-07-11 17:21  sysgit  阅读(280)  评论(0编辑  收藏  举报