Python学习笔记1


新手前来报到,希望和大家一起进步!
Python结尾竟然不需要分号啥的,感觉太智能了。。。
APPLE = 1000  #全局变量
print(2**3)  # **表示多少次方,example:2的3次方
print('Python 练习')
apple = 1
print(apple)
a = 4
b = 5
c = 6
print(a, b, c)
# while循环
while a<10:
        print('a =',a )
        a = a + 1
#for循环
example_list = [1,2,3,4,5,6,7,8,9,48,78,566,54]
for i in example_list:
    print(i)
    print('inner the for')
print('out of for')

for i in range(1,10,2):  # range 不输出最后一位,第三个是步长
    print(i)
#if判断语句
x = 1
y = 2
z = 3
if x<y:
    print('x is less than y')
if z>y:
    print('z is larger than y')
if x!=y:
    print('x is not equal to y')
if x>y:
    print('x is larger than y')
else:
    print('x<=y')

if x>y:
    print('x>y')
elif x>y:
    print('x>z')
else:
    print('x<y,x<z')
    print('finish running')

def function():
    print('This is a function')
    a=1+2
    print(a)
def fun(a,b):
    c=a*b;
    print('the c is',c)
fun(10,20)
def sale_car(price,color,brand,second_hand,):
    print('price is',price,
          'color is',color,
          'is_second_hand',second_hand,)
sale_car(1000,'blue','carmy',True)
def fun2():
    a=10
    print(a)
    return a+100
print(fun2())
#定义全局变量时需要在外面用全大写
#a=None
#函数里面 global a
text = 'This is my first test.\nThis is next line.\nThis is last line.'
print(text)
my_file = open('my file.txt','w')
my_file.write(text)
my_file.close()
#追加文字
append_text = '\nThis is append file.'
my_file = open('my file.txt','a')
my_file.write(append_text)
my_file.close()
#
file = open('my file.txt','r')
content = file.read()
print(content)
#
file = open('my file.txt','r')
content = file.readline()
#content = file.readlines()
#python_list = [1,2,3,4,5]
print(content)
8
Python 练习
1
4 5 6
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
1
inner the for
2
inner the for
3
inner the for
4
inner the for
5
inner the for
6
inner the for
7
inner the for
8
inner the for
9
inner the for
48
inner the for
78
inner the for
566
inner the for
54
inner the for
out of for
1
3
5
7
9
x is less than y
z is larger than y
x is not equal to y
x<=y
x<y,x<z
finish running
the c is 200
price is 1000 color is blue is_second_hand True
10
110
This is my first test.
This is next line.
This is last line.
This is my first test.
This is next line.
This is last line.
This is append file.
This is my first test.




Process finished with exit code 0

class Calculator:
    name = 'Good calculator'
    price = 18

    def add(x, y):
        self.name = 'Zhang'
        print(name)
        result = x+y
        print(result)

    def minus(self, x, y):
        result = x-y
        print(result)

    def times(self, x, y):
        result = x*y
        print(result)

    def divide(self, x, y):
        result = x/y
        print(result)


Calcu = Calculator()
Calcu.name
Calcu.price
Calcu.minus(6, 8)
Calcu.times(4, 3)

"D:\software dat。。。。。。。。。。。。。。。。。。
-2
12


Process finished with exit code 0

class Calculator:
    name = 'Good calculator'
    price = 18


# def __init__(self, name, price, hight, width, weight):
    def add(x, y):
        self.name = 'Zhang'
        print(name)
        result = x+y
        print(result)

    def minus(self, x, y):
        result = x-y
        print(result)

    def times(self, x, y):
        result = x*y
        print(result)

    def divide(self, x, y):
        result = x/y
        print(result)


Calcu = Calculator()
Calcu.name
Calcu.price
Calcu.minus(6, 8)
Calcu.times(4, 3)
Caclu2 = Calculator()
a_input = input('请输入一个数字:')      #返回的是字符串
print('用户输入的数是:', a_input)
"D:\software dat........................
-2
12
请输入一个数字:54
用户输入的数是: 54


Process finished with exit code 0

a = [4, 5, 9, 54, 6, 35, 14]
a.append(789)  #添加789
print(a)
a.insert(3, 888)
print(a)       # 记住Python是从0开始的
a.remove(54)   # 去除掉第一个54,
print(a)
print('1:', a[0:3])
print('2:', a[3:5])
print('3:', a[-3])
print('4:', a[-3:-1])
print('5:', a[0])
print('6:', a[-0])
print('7', a[-1])
a.sort()
print(a)
a.sort(reverse=True)
print(a)
"D:\software date\python data\.............................................
[4, 5, 9, 54, 6, 35, 14, 789]
[4, 5, 9, 888, 54, 6, 35, 14, 789]
[4, 5, 9, 888, 6, 35, 14, 789]
1: [4, 5, 9]
2: [888, 6]
3: 35
4: [35, 14]
5: 4
6: 4
7 789
[4, 5, 6, 9, 14, 35, 789, 888]
[888, 789, 35, 14, 9, 6, 5, 4]


Process finished with exit code 0
# 提及numpy pandas 教程
# 本节多维list
a = [1,2,3,4,5]
multi_dim_a = [[1,2,3],
               [2,3,4],
               [3,4,5]]
print(a[1])
print(multi_dim_a[1][1])
print('字典,字典是一个没有顺序的容器')
b = {'apple': 1, 'pear': 2, 'orange': 3}
print(b['apple'])
print(b['pear'])
del b['pear']
b['zhang'] = 23
print(b)
"D:\software date\python data\venv\Scripts\python.exe" "D:/software date/python data/python1.5.py"
2
3
字典,字典是一个没有顺序的容器
1
2
{'apple': 1, 'orange': 3, 'zhang': 23}


Process finished with exit code 0
# continue break
a = True
while a:
    b = input('type something')
    if b == '1':
        a = False
    else:
        pass
        print('still in while')  # Python特别强调结构,四个空格特别需要注意

print('finish run')
"D:\software date\python data\ven......................................
type something21
still in while
type something1
finish run


Process finished with exit code 0
# zip lambda map 的使用
a = [1, 2, 3]
b = [4, 5, 6]
zip(a, b)
list(zip(a, b))
print(list(zip(a, b)))
for i,j in zip(a,b):
    print(i/2, j*2)
print(list(zip(a, a, b)))

def fun1(x, y):
    return (x + y)
c1 = fun1(2, 5)
print(c1)
fun2 = lambda x, y: x + y     # lambda此处作用就是简写函数!
c2 = fun2(2, 5)
print(c2)

map(fun1, [3], [5])
print(list(map(fun1, [3], [5])))
map(fun1, [1, 3],[2, 5])
print(list(map(fun1, [1, 3],[2, 5])))
"D:\software date\python data\.........................................
[(1, 4), (2, 5), (3, 6)]
0.5 8
1.0 10
1.5 12
[(1, 1, 4), (2, 2, 5), (3, 3, 6)]
7
7
[8]
[3, 8]


Process finished with exit code 0
# set 的使用
char_list = ['a', 'a', 'b', 'b', 'c', 'd']
print(set(char_list))   #set类型,无序的
print(type(set(char_list)))
print(type({1: 2}))  # 返回类型为字典类型,与上面类型不同
sentence = 'Welcome Back To This Class'
print(set(sentence))
b = set(sentence)
b.add('x')
print(b)
"D:\software date\python data\venv...................................
{'b', 'a', 'c', 'd'}
<class 'set'>
<class 'dict'>
{' ', 'B', 'o', 'a', 'e', 's', 'k', 'm', 'W', 'i', 'c', 'T', 'h', 'l', 'C'}
{' ', 'B', 'o', 'a', 'e', 's', 'k', 'm', 'W', 'i', 'c', 'T', 'h', 'l', 'x', 'C'}


Process finished with exit code 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值