介绍,数据类型,循环

一、介绍

python部分以python3为基础进行阐述,python强制缩进,不依赖于别的符号来划分区域,例如C语言的{ }。

print("Hello World!")

python  C:UsersAdministratorDesktop est.py

执行python代码的步骤:

1.先启动python解释器

2.python解释器把文件内容从硬盘读入内存,不涉及程序的执行

3.读入内存后,再解释执行

执行python代码的两种方式

        交互式

        优点:即时调试程序,调试方法

        缺点:无法永久保存代码

        文件方式

        优点:永久保存代码

        缺点:不能即时调试代码

二、变量

python没有常量的概念,一切皆可以是变量,但习惯性的用全大写字母组成的字符代表常量AGE_OF_RAP = 56

变量是指可变化的量,可用来代指内存里某个地址中保存的内容。有如下规则:

1.变量名只能是字母、数字或下划线的任意组合

2.变量名的第一个字符不能是数字

3.以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

python中的变量名没有储存值的功能,只是一种绑定关系,当把x=10更改为x=11时,只是把x和10的

绑定关系解除掉,把x绑定到11这个值上。

>>>x=10
>>>id(x)
1581299040
>>>x=11
>>>id(x)
1581299072

两个id不同,说明是开辟了新的内存空间,那么现在10这个内存空间没有了任何绑定关系,python解释器会回收这个没有绑定名字的变量。

一个值绑定一个名字称为引用计数+1,什么是引用计数?x就是11这个值的引用,那么11这个值可不可以再来一个引用的名字?

>>>x
11
>>>y=x
>>>id(y)
1581299072

和上面x的id一样,说明y这个名字也绑定到了11身上,现在11身上有两个名字绑定,那么11的引用计数为2,当引用计数为0的时候会被清理掉,那么什么时候会被清理掉呢?没有任何绑定的时候,如何才能没有绑定呢?

解除绑定关系的方式

x=100
y=200

这样一来,x与10的绑定,y与11的绑定都去掉了,10和11变为了没有任何绑定关系的内存空间了可以被清除掉了。

另一种解除绑定关系的方式

del x

这叫解除绑定,不叫删除

三、字符串

字符串,由单引号或双引号组成,如'i love python'或"i love python",具有不可变性,因此一旦建立便不能修改

>>>msg='I love python'
>>>msg
'I love python'
>>>msg.split()
['I','love','python']

字符串的拼接

>>>msg1='hello'
>>>msg2='world'
>>>msg3=msg1+msg2
'helloworld'
>>>msg4=msg1*3
>>>msg4
'hellohellohello'
>>>print('hello'+' world') # + 少用
hello world

四、读取用户输入

python3弃用了raw_input(),只用input()进行读取用户的输入

username = input("What is your name?")

格式化输出

>>>name='tom'
>>>print('my name is ',name)
my name is tom
>>>
>>>name='tom'
>>>age=18
>>>print('my name is %s, my age is %s' %(name,age))
my name is tom, my age is 18

五、列表

可以存放多个值,要取得列表内的元素,需要通过下标,从0开始

>>>list1 = ['physics', 'chemistry', 1997, 2000]
>>>list2 = [1, 2, 3, 4, 5 ]
>>>list3 = ["a", "b", "c", "d"]
>>>list4 = [1,'a',[2,3]]
>>>list4[0]
1
>>>list4[2][1]
3

六、字典

采用key:value方式存储值,可以存放多个值,每个也可以是任意数据类型,但key必须是不可变数据类型

可变数据类型:列表,字典

    id不变

    type不变

    值改变

不可变类型:value一旦改变,id也改变,如数字,字符串(id变,意味着创建了新的内存空间)

>>>d={'a':1}
>>>id(d)
31214184
>>>d['a']=2
>>>id(d)
31214184
>>>d
{'a':2}
#值改变,id不变
>>>num=10
>>>id(num)
1349825888
>>>num=11111
>>>id(num)
5743728
>>>
>>>s='abc'
>>>id(s)
31149280
>>>s='def'
>>>id(s)
32025656

#值改变,id也变
>>>dict1 = { 'abc': '456' }
>>>dict2 = { 'abc': 123,'98.6':'37' }
>>>dic2['abc']
123
>>>dic2['count'] = 2
>>>dic
{'abc': 123,'98.6':'37','count':2}

三种成员设计形式

>>>users=[['tom','123'],['jerry':'123']]
>>>users[0]
['tom','123']
>>>users[0][1]
'123'
>>>
>>>users=[{'tom':'123'},{'jerry':'123'}]
>>>users[1]{'tom'}
'123'
>>>
>>>users={'tom':'123','jerry':'123'}
>>>users['tom']
'123'
>>>'jerry' in users
True
>>>

 七、流程控制

x = int(input("请输入您的总分:"))
if x >= 90:
    print('')
elif x>=80:
    print('')
elif x >= 70:
    print('')
elif x >= 60:
    print('合格')
else:
    print('不合格')

八、循环

#!/usr/bin/python
 
count = 0
while count < 9:
   print 'The count is:', count
   count = count + 1
 
print "Good bye!"
age=50
count=1
while count <=3:
    age_input=int(input('input age: '))
    if age_input > age:
        print('too big')
        count +=1
    elif age_input < age:
        print('too small')
        count +=1
    else:
        print('got it')
        break
count = 0
while count <= 10:
    if count % 2 != 0:
        count+=1
        continue
    print(count)
while True:
    cmd = input('>>:')
    if cmd == 'q':
        break
count=0
while True:
    if conut > 2:
        print('too many tries')
        break
    user=input('user: ')
    password=input('password: ')
    if user == 'tom' and password == '123':
        print('Login successful')
        while True:
            cmd=input('>>: ')
            if cmd == 'q':
                break
            print('exec %s' % cmd)
        break
    else:
        print('Login error')
        count+=1
count=0
tag=True
while tag:
    if conut > 2:
        print('too many tries')
        break
    user=input('user: ')
    password=input('password: ')
    if user == 'tom' and password == '123':
        print('Login successful')
        while tag:
            cmd=input('>>: ')
            if cmd == 'q':
                tag=False
                continue
            print('exec %s' % cmd)
    else:
        print('Login error')
        count+=1
count=1
while count <= 5:
    print(count)
    count+=1
else:              #while没有被break打断的时候才执行else的子代码
    print('====')

九、运算符

算术运算

比较运算

逻辑运算

原文地址:https://www.cnblogs.com/Ryans-World/p/5820381.html