python学习笔记

'''
#判断
a = '2'
if a == '1':
print('a == 1')
elif a == '2':
print('a == 2')
else:
print('a 等于其它')

#字符串比较
name1 = 'Jack'
name2 = 'Jack'

if name1 == name2:
print('名字相同')
else:
print('名字不相同')


#for循环
names = ['Jack', 'Tom', 'Mike']
for name in names:
print(name)

#range可以生成一个整数序列,list将整数序列转换为列表
sum = 0
for i in list(range(101)):
sum += i
print(sum)

#while循环
sum = 0
num = 99
while num > 0:
sum += num
num = num - 2
print(sum)

#break语句
n = 1
while n < 99:
if(n > 10):
break
print(n)
n += 1

#continue语句
n = 0
while n < 10:
n += 1
if(n % 2 == 0):
continue
print(n)

========================================
#函数定义
def my_len(str):
length = 0;
for c in str:
length += 1
return length

length = my_len("http://www.baidu.com")
print(length)

#函数可变参数
def plus(*numbers):
add = 0;
for i in numbers:
add += i
return add

print(plus(1,2,3,4))
print(plus(1,2,3,4,5))

#函数可变参数
def plus(*numbers):
add = 0;
for i in numbers:
add += i
return add

print(plus(1, 2, 3, 4))
print(plus(1, 2, 3, 4, 5))

========================================
#列表
numbers_list = [2, 4, 6, 8, 10]
#索引
print(numbers_list[0])
#长度
print(len(numbers_list))
#连接列表
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = list1 + list2
print(list3)
#列表截取
sub_list = [1, 2, 3, 4, 5, 6]
print(sub_list[2: 4])
#append方法,增加元素
apd_list = [1, 2, 3, 4]
apd_list.append(5)
print(apd_list)
#index列表索引,输出列表索引
index_list = [1, 2, 3, 4, 5, 6]
for i in index_list:
print(index_list.index(i))
#insert方法, 在索引2的位置插入9
#输出结果[1, 2, 9, 3, 4, 5, 6]
index_list.insert(2, 9)
print(index_list)
#删除插入的9
index_list.remove(index_list[2])
print(index_list)

========================================
#元组
my_tuple = (1, 2, 3, 4, 5, 6, 7)
print(my_tuple)

names = ('Jack', 'Tom', 'Mark', 'Sue', 'Holly', 'Kelly')
for name in names:
print(name)

for i in range(len(names)):
print(names[i])

#元组和列表转换
mumber_tuple = (1, 2, 3, 4, 5, 6, 7)
mumber_list = list(mumber_tuple)
print(mumber_list)

str_list = ['Hello', 'World', 'Python']
str_tuple = tuple(str_list)
print(str_tuple)

#字符串操作
#for循环迭代字符串
str = 'python'
for char in str:
print(char)

#索引
my_string = 'Hello World'
print(my_string[0], my_string[6], my_string[10])

#len函数
string = 'python'
print(len(string))

#连接字符串
first_name = 'Tom'
last_name = 'Mark'
full_name = first_name + ' ' + last_name
print(full_name)

#字符串切片
my_string = 'Hello World'
cut_string = my_string[6:10]
print(cut_string)

#搜索字符串
names = ('Jack', 'Tom', 'Mark', 'Sue', 'Holly', 'Kelly')
if 'Jack' in names:
print('Jack在字符串中被找到')
else:
print('Jack在字符串中没有被找到')

#面向对象编程
#封装
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def eat(self):
print("{0}, {1}, {2}, 吃饭".format(self.name, self.age, self.gender))
def drink(self):
print("{0}, {1}, {2}, 喝水".format(self.name, self.age, self.gender))
def sleep(self):
print("{0}, {1}, {2}, 睡觉".format(self.name, self.age, self.gender))

person1 = Student('Jack', 18, '')
person1.eat()
person1.drink()
person1.sleep()

#继承
class Animal:
def __init__(self):
self.name = None
def eat(self):
print("{0}在吃饭".format(self.name))
def drink(self):
print("{0}在喝水".format(self.name))
def sleep(self):
print("{0}在睡觉".format(self.name))

class Cat(Animal):
def __init__(self, name, age):
super().__init__()
self.name = name
self.age = age
def introduce(self):
print("小猫的名字是{0},年龄是{1}".format(self.name, self.age))
def catch_mouse(self):
print("小猫{0}在抓老鼠".format(self.name))
def jump(self):
print("小猫{0}在跳高".format(self.name))

class Dog(Animal):
def __init__(self, name, age):
super().__init__()
self.name = name
self.age = age
def introduce(self):
print("小狗的名字是{0},年龄是{1}".format(self.name, self.age))
def catch_mouse(self):
print("小狗{0}在看门".format(self.name))
def swimming(self):
print("小狗{0}在游泳".format(self.name))

cat = Cat("小花", 7)
cat.introduce()
cat.drink()
cat.sleep()
cat.eat()
cat.jump()
'''
posted @ 2024-07-09 14:45  幸福在靠近  阅读(1)  评论(0编辑  收藏  举报