一、Python基础(数据类型、基本函数、基本运算)

1.变量

作用:为了简便,运算时方便修改运算中的值,代指一些复杂过长的数据;

what:用变量代指一些内容;

how:全部由字母、数字和下划线组成,数字不能开头,不能和Python关键词重复;有一定的描述性好识别(驼峰体:AgeOfStudent;下划线体:age_of_student);不能过长;

2.常量

在Python里常量是特殊的变量,只是为了迎合其他语言才有了常量的概念,特指不变的数据(π、等等);

3.注释

单行:#

多行:'''……'''

作用:解释代码;

4.基础数据类型

int:整数

float:浮点数

bool:布尔型(TRUE/FLASE)

str:字符串

tuple:元祖,只读不能修改

list:列表

dict:字典

set:集合

5.基本函数

input:username = input('请输入用户名')

流程if:①基本结构:if 条件:

           结果

score = 59if score <60:  print('你没有及格')

    ②if...elif...else

score = 60if score <60:  print('你没有及格')else:  print("你及格了")  if score >=60 and score < 80:    print("良")  elif score >=80 and score <90:    print("优")  else:    print("牛啊,达到了90分上了")

while循环:①基本结构:while 条件:

                                                 循环体

      ②中断:更改条件、break、系统命令、continue

      ③用处:重复之前的动作,例如输入用户名密码;

count = 0
while count < 10print(count)
    count +=1
#打印小于10的整数
#输出结果
#0
#1
#2
#3
#4
#5
#6
#7
#8
#9
View Code

 

for循环:有限循环,可用作字符串、字典、元组、文件的切片打印;

name = ['sunhao','sunchen','sunxu']
for i in name:
    print(i)

#打印结果
#sunhao
#sunchen
#sunxu

  

6.基本运算符

算数运算符:+、-、*、/、**、//、%

比较运算符:>=、<=、==、<、>、!=

赋值:=、+=、-=

逻辑运算符:and、or

成员运算符:in、not in

身份运算符:is、is not

位运算(二进制):太麻烦,先不管,也不怎么用得上呢!-------->& 按位与、|  按位或、^ 抑或运算、~ 按位取反、<<左移  a<<c  == a*(2**c)、>>右移  a>>c  == a/(2**c)

a = 1
b = 2
c = 3
d = 4
print(a + b)     #结果是3
print(a-b)       #结果是-1
print(b*c)       #结果是6
print(d/b)       #结果是2
print(c**b)      #乘方结果是9
print(d//c)      #商结果是1
print(d//c)      #余数结果是1
print(a<b)       #结果True

  

7.数据类型之间的转换

int-->str:a = str(123)

str-->int:b = int('123')

int-->bool:0代表False,1代表True

bool-->int:

byte-->str:

str-->byte:

posted @ 2020-03-30 13:31  坑爹的川川  阅读(365)  评论(2)    收藏  举报