第一节-Python的安装及基本语法

1、存储二进制文件:a = b"Hello World"    //加b
2、将字符型编码成二进制数据,
b = "Hello World"
print(b)                   //Hello World
pring(type(b))        //<class 'str'>
print(b.encode())//二进制

课后题答案:
1、
2、print("余生请多多指教")
3、a = "余生请多多指教"
      print(a.encode())
4、一、import math
             print(log2(1024))
     二、from math import log2
            print(log2(1024))
5、from math import log2
     print(log2(6259999))
6、shift + F10             //运行当前文件
      Alt  + shift + F10   //运行可选文件

选择题
7、
print(math.floor( 3.14))
print(math.floor( 3.92))
print(math.fabs(- 3.0))
print(math.fmod( 5 , 3))
print( abs( 3))
print( abs(- 3.0))
print( round ( 1.234))
print( round ( 1.234 , 2))
print(math.sqrt( 9))
print(math.pow( 9 , 2))

3
3
3.0
2.0
3
3.0
1
1.23
3.0
81.0

8、
print(math.pi)

test = "余生请多多指教"
print(test.encode( encoding= "utf-8"))
a = test.encode( encoding= "utf-8")
print(a.decode( encoding= "utf-8"))

test = "余生请多多指教"
print(test.encode( encoding= "gb2312"))
a = test.encode( encoding= "gb2312")
print(a.decode( encoding= "gb2312"))

print(math.pow(math.exp( 1) , 2))
print( "+")
print(math.sqrt(math.pi))

3.141592653589793
b'\xe4\xbd\x99\xe7\x94\x9f\xe8\xaf\xb7\xe5\xa4\x9a\xe5\xa4\x9a\xe6\x8c\x87\xe6\x95\x99'
余生请多多指教
b'\xd3\xe0\xc9\xfa\xc7\xeb\xb6\xe0\xb6\xe0\xd6\xb8\xbd\xcc'
余生请多多指教
7.3890560989306495
+
1.7724538509055159
上课回顾:
  • System interpreter设置告诉PyCharm如何理解你的代码
  • Python 3的编码
  1. 字节(序列)bytes:对应decode(解码)
  2. 字符(串)str:对应encode(编码)
  • Debug方法:
  1. 肉眼观察法(字母拼写错误、遗漏符号、语法错误等)

本节课内容:
1、基本数据类型和运算符
  • 表示数字
  • 表示文字
  • 基本数据结构
  • 列表 List
  • 字典 Dict
  • 元组 Tuple
  • 集合 Set
2、Python中的几类字符串变量
3、流程控制
  • 条件语句
  • 循环语句
4、流程干涉
  • break语句
  • continue语句
------------------------------------------------------------------
基本数据类型

int 
整数
6、100

float
浮点数、小数
6.0、2.142

bool 
布尔值
True、Flase

complex
 复数
3+4j、complex(3,4)
实部虚部默认浮点数
  • 算术运算
  1. 加减乘除
  2. 乘方:2**10=1024
  3. 取模 取余
  • 取模:13//6 #结果是2 
  • 取余:13%6 #结果是1
  1. math模块
  • from math import log2,sqrt

  • 赋值运算符(一边运算一边赋值)
  • a = a + 6可以写成 a += 6
  • =、+=、-=、*=、/=、**=、//=、%=

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

  • 逻辑运算符
  • and、or、not

  • 位运算符(针对二进制进行运算)
  • &与、I或、~

  • 成员运算符
  • in、not in

  • 身份运算符(返回True or Flase)
  • is、is not

运算符的优先级:
  • Python 3的几类字符串
  1. 字符串
  2. 字节序列
  3. Unicode字符串


  • 流程控制
  1. 为什么需要流程干涉?
  • 条件语句if、if else、if elif else
  • 循环语句while、for
  •  综合使用 之 死循环
当我们的流程出现我们不希望出现的情况时,我们引入流程干涉;
  1. 首先有一个流程
  2. 满足特定条件
  3. 用break、continue
1、
a = 0
sum = 0
while a <= 100:
a += 1
if a% 2 == 0:
sum += a
continue
print(sum)

#结果:2550
2、
list1 = [ "Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat"]
list2 = [ "Sun" , "Sat"]
for i in list1:
if i not in list2:
print(i)

#结果:
Mon
Tue
Wed
Thu
Fri

3、 编程计算: 0101 与 0011 的三种位运算结果(与, 或, 非)
b = 0b0101
c = 0b0011
print( bin(b&c))
print( bin(c|b))
print( bin(~b))
print( bin(~c))

#结果:
0b1
0b111
-0b110
-0b100

4、如何计算10的阶乘

5、
q = 1
for q in range( 0 , 5) :
w = 1
print( "")
for w in range( 0 , 5):
if ( 2>= abs(q-w)>= 0)&((q+w)>= 2)&((q+w)<= 6):
print( "*" , end= "")
w += 1
else:
print( " " , end= "")
q += 1

结果:
  *  
 ***
*****
 ***
  *
#q = 1
for q in range ( 0 , 5 ) :
#w = 1
print ( "" )
for w in range ( 0 , 5 ):
if ( 2 >= abs (q-w)>= 0 )&((q+w)>= 2 )&((q+w)<= 6 ):
print ( "*" , end = "" )
w += 1
else :
print ( " " , end = "" )
q += 1
红色加粗注释掉后,显示效果未有变化,
思考,python里for p in range()实现,每次从起循环到尾自动完成,前开后闭 
#q = 1
for q in range( 0 , 5) :
#w = 1
print( "")
for w in range( 0 , 5):
if ( 2>= abs(q-w)>= 0)&( 6>=(q+w)>= 2):
print( "*" , end= "")
#w +=1
else:
print( " " , end= "")
#q +=1
结果依然
实验证明rang()是左闭右开
故:rang(0,5)取值0、1、2、3、4

别人更好的方法:
for i in range ( 1 , 6 ):
star_num= 5 - 2 * abs (i- 3 )
black_num= abs (i- 3 )
print ( " " *black_num+ "*" *star_num)

猜你喜欢

转载自blog.csdn.net/sdhotn/article/details/80323165
今日推荐