python-1 list tuple

修改 pip 为国内源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
或 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

pip install jupyter
jupyter notebook

Python的语言类型

Python 是动态语言、强类型语言

静态编译语言:事先声明类型,类型不能再改变,编译是检查 C C++
动态编译语言:不用事先声明类型,随时可以复制为其他类型。编程时不知道是什么类型,很难推断

字符串 + 数字
强类型语言:不同类型之间操作,必须先强制类型转换为同一类型 print('a'+1)
弱类型语言:不同类型间可以操作,自动隐式转换 JavaScript 中 console.log(1+'a')

原码、反码、补吗,负数表示法
原码:5 => 0b101 , 1 => 0b1 ,-1 => -0b1,bin(-1)
反码:正数的反码与原码相同;负数的反码符号位不变其余按位取反
补码:正数的补码与原码相同;负数的补码符号位不变其余按位取反后 +1
负数表示法:
早起数字电路的CPU中的运算器实现了加法器,但是没有减法器,减法要转换成加法

负数在计算机中使用补码存储,-1 的补码为 1111 1111

5 - 1 => 5 + (-1) 直觉上是 0b101 - 0b1,其实计算机中是 0b101 + 0b 11111111,溢出位舍弃
0000 0101 5的原码
1000 0001 -1的原码
1111 1111 -1的补码
0000 0101 + 1111 1111 => 1 0000 0100 溢出位舍弃得 4

~12 为什么是 -13?
0000 1100 12的原码
1111 0011 取反后的补码 最高位位1 位负数 为了给人看转为原码
1000 1100 + 1 => 1000 1101 => -13

10 ^9 等于? 10^-9等于? 为什么
0000 1010
0000 1001
0000 0011 => 3

enter description here

逻辑运算符
与或非 and or not
短路运算符
and 如果前面的表达式等价为False,后面就没有必要计算了,这个逻辑表达式最终一定等价为False
1 and '2' and 0 => 0
0 and 'abc' and 1 => 0
or 如果前面的表达式等价为True,后面没有必要计算了,这个逻辑表达式 最终一定等价为True
1 or False or None => 1

运算符
赋值运算符: 先算右边后再赋值给左边变量
a = min(3,5)
+= -= *= /= %= //= 等
x = y = z = 10

成员运算符:
in 、 not in
身份运算符:
is 、is not

enter description here

表达式Expression
由数字、符号、括号、变量等的组合
算数表达式
逻辑表达式
赋值表达式 python中 ,赋值即定义 ,如果一个变量已经定义,赋值相当于重新定义

内存管理
enter description here

enter description here

flag = True
draw = 0 
conuter = 0 
while flag:
    temp = input("输入:")
    if temp == "": 
        flag = False 
    else: 
        print("shuru:",temp)
        draw += int(temp) 
        conuter += 1  
print("输入个数:",conuter, "平均数:",draw/conuter)


n = int(input("输入:"))
for i in range(n):
    if i == 0 or i == n-1 :
        for j  in range(n-1):
            print("+",end=" ")
        print("+")
        continue
    for j in range(n):
        if j == 0 :
            print("+",end=" ")
        elif j == n-1:
            print("+")
        else :
            print("-",end=" ")

num = 1 
add = 0
for i in range(5):
    for j in (1,i+1) : 
        num = num * j
    add += num 
print(add)
			
num = 3
for i in range(2,num):
    if num % i :
        print("不是素数")
        break
else:
    print("这个数是素数!") 
	
x = 1 
y = 1 
x , y = y , x+y 
print(x, y )

for 循环中的 else 关键字指定循环结束时要执行的代码块

round(-2.5) 4舍 6入 5取偶

类型判断
type(obj),返回类型,而不是字符串
isinstance(obj, class_or_tuple),返回布尔值

列表
一个列队,一个排列整齐的队伍

链表

列表查询
L9.index(10) 查询到一个后就不再遍历 括号中是value
随着列表中元素的增加, index 函数的效率下降
随着列表元素规模的增加,性能下降
有 n 个,时间复杂度,O(n)

L1.count(100) 计数 全部遍历 O(n) 括号中是value

enter description here

L1.extend(rang(7,10))

[[1]] # [address:401] 401住着1 
# 1 、‘abc’ 字面常量,称为简单类型
# [1] 复杂类型,应用类型
l1 = [[1]] * 5  # [ a401 , a401, a401, a401, a401]  [[1],[1],[1],[1],[1]]
l1[0][0] = 100  # 修改的是 a401地址存储的值
l1 
[[100], [100], [100], [100], [100]]

enter description here

enter description here

三目运算符
enter description here

n = 5 
for i in range(n):
    if i == 0 or i == n-1:  # 或     if i % (n-1) == 0 : 
        print( '*' * n )
    else :
        print('*' + ' ' * (n - 2) + '*' )
		

n = 8 
for i in range(n):
    line = '*' * n if i == 0 or i == n-1 else '*' + ' ' * (n-2) + '*'
    print(line)    

九九乘法表

for i in range(1,10):
    for j in range(1,10):
        if j <= i :
            sum = i * j 
            print("%s*%s=%s " %(j,i,sum),end=" | ")
    print("")
	
	
for i in range(1,10):
    line = ''
    for j in range(1,i+1): 
        line += str(j) + '*' + str(i) + '=' + str(i*j) + ' '  
    print(line)

enter description here

enter description here

enter description here
{2:<2} 2对应 i * j , :<2 冒号是分割符号,<表示左对齐,2表示宽度

enter description here

enter description here

打印菱形

     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

=================================================
n = 5 
for i in range(1,n+1):
    print( " " * (n+1-i) + "*" * (i*2-1))
for i in range(n-1,0,-1):
    print( " " * (n+1-i) + "*" * (i*2-1))
	
=================================================
n = 7
m = int((n+1) /2)
for i in range(1,n+1):   
    if i <= m :
        print(" " * (m-i) + "*" * (i*2-1))
    else:
        print(" " * (i-m) + "*" * ((n+1-i)*2 - 1))
		
================================================
       行     *      " "空格
1      -3     1       3  
2      -2     3       2 
3      -1     5       1 
4      0      7       0
5      1      5       1 
6      2      3       2 
7      3      1       3
			7-2*e     e 

n = 7 
e = n // 2 
for i in range(-e,n-e):
    prespaces = -i if i<0 else i 
    print(' ' * prespaces ,end='')
    print('*' * (n - 2*prespaces))
===================================================

enter description here

enter description here

闪电

斐波那契数列

a = 1
b = 1 
print(a,b,sep='\n')

while True:
   c = a + b 
   if c >=100 : break 
   a = b
   b = c 
   print(c)
   

a = 1
b = 1 
conut = 2 
# print(a,b,sep='\n')

while True:
   c = a + b 
   conut += 1 
   if conut >=101 : 
       print(c)   # 打印第101项
       break 
   a = b
   b = c 

573147844013817084101


# 开放点
conut = 0 
for x in range(2,100000):
   for i in range(2,int(x**0.5)+1):
       if x % i == 0 :
           break 
   else: 
       conut += 1 
print(conut)   # 9592


# 开放点,去偶数
conut = 1 
for x in range(3,100000,2):
   for i in range(2,int(x**0.5)+1):
       if x % i == 0 :
           break 
   else: 
       conut += 1 
print(conut)


conut = 1 
for x in range(3,100000,2):
   for i in range(3,int(x**0.5)+1,2):
       if x % i == 0 :
           break 
   else: 
       conut += 1 
print(conut)

=====================
import datetime

times = 100000
start = datetime.datetime.now()
conut = 1 
for x in range(3,times,2):
   if x > 10 and x % 5 == 0 :
       continue
   for i in range(3,int(x**0.5)+1,2):
       if x % i == 0 :
           break 
   else: 
       conut += 1 
delta = (datetime.datetime.now() - start).total_seconds() 
print(conut,delta)

enter description here

from collections import namedtuple
Point = namedtuple('Point' , ['x','y'])
p1 = Point(4, 5)   # 一旦赋值 ,不可修改
p1.x , p1.y  # (4, 5)

Student = namedtuple('Student','name age')
tom = Student('tom',20)
jerry = Student('jerry',18)
tom.name , jerry.age  # ('tom', 18)

tom.age = 22
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_2116\1247458839.py in <module>
----> 1 tom.age = 22

AttributeError: can't set attribute

=================
class A : 
    def __init__(self, x, y):
        self.x = x 
        self.y = y 
    def __repr__(self):
        return "A (x={}, y={})".format(self.x , self.y)
		
a = A(5,6)	
a.x , a.y  # (5, 6)

=================

排序
enter description here

enter description here

enter description here

nums = [6,5,4,8,3,2,9,1,7,0]
length = len(nums)

for i in range(length-1):
    for j in range(length-1-i):  
        if nums[j] > nums[j+1]:
            temp = nums[j]
            nums[j] = nums[j+1]
            nums[j+1] = temp  
            # nums[j], nums[j+1] = nums[j + 1], nums[j]
print(nums)

enter description here

enter description here

posted @ 2022-04-20 18:03  何时&明月  阅读(33)  评论(0)    收藏  举报