如何快速的学习完python
a = 1
b = 2.0
c = True
d = "hello"
e = [1, 2, 3]
f = (1, 2, 3)
g = {"name": "Alice", "age": 20}
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
for i in range(5):
print(i)
j = 0
while j < 5:
print(j)
j += 1
def add(x, y):
return x + y
result = add(3, 4)
print(result)
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_dict = {"name": "Bob", "age": 30}
with open("file.txt", "w") as f:
f.write("Hello, world!")
with open("file.txt", "r") as f:
contents = f.read()
print(contents)
try:
result = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
import math
result = math.sqrt(16)
print(result)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print("Hello, my name is", self.name)
person1 = Person("Alice", 20)
person1.say_hello()
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
def say_hello(self):
print("Hello, my name is", self.name, "and I am in grade", self.grade)
student1 = Student("Bob", 18, 12)
student1.say_hello()
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount > self.__balance:
print("Insufficient funds")
else:
self.__balance -= amount
def get_balance(self):
return self.__balance
account1 = BankAccount(1000)
account1.deposit(500)
account1.withdraw(200)
print(account1.get_balance())
class Flyable:
def fly(self):
print("I am flying")
class Animal:
def __init__(self, name):
self.name = name
class Bird(Animal, Flyable):
pass
bird1 = Bird("Eagle")
bird1.fly()