name = '函数使用'
def say_hello ( ) :
print ( '1' )
print ( '12' )
print ( '123' )
say_hello( )
print ( name)
def sum ( num1, num2) :
res = num1 + num2
print ( res)
sum ( 1 , 2 )
def sum1 ( num1, num2) :
return num1 + num2
result = sum1( 2 , 3 )
print ( result)
def test ( ) :
print ( '*test' * 50 )
def test1 ( ) :
print ( 'test11' )
test( )
print ( '_' * 50 )
test1( )
import python模块
print ( python模块. name)
python模块. test( )
class Student :
def study ( self, course_name) :
print ( f'学生正在学习 { course_name} ' )
def play ( self) :
print ( '学生在玩游戏' )
stu1 = Student( )
stu2 = Student( )
print ( stu1)
print ( stu2)
print ( hex ( id ( stu1) ) , hex ( id ( stu2) ) )
stu1. study( '学习python' )
study1 = Student( ) . study( 'java学习' )
class Student :
def __init__ ( self, name, age) :
self. name= name
self. age= age
def study ( self, course_name) :
print ( course_name)
def play ( self) :
print ( self. name, self. age)
stu = Student( '小明' , 18 )
stu. play( )
stu. study( 'python' )
class Student11 :
def __init__ ( self, name, age) :
self. _name= name
self. _age= age
@property
def study ( self, course_name) :
print ( course_name)
def play ( self) :
print ( self. _name, self. _age)
stu = Student11( '小明' , 18 )
stu. play( )
class Animal :
def get ( self) :
print ( '获取' )
def set ( self) :
print ( '设置' )
class Dog ( Animal) :
def brak ( self) :
print ( '叫' )
dw= Dog( )
dw. get( )
class Car ( ) :
def __init__ ( self, make, model) :
self. make = make
self. model = model
self. odometer = 0
def get_name ( self) :
long_name = f" { self. make} { self. model} "
return long_name. title( )
def read_odometer ( self) :
print ( f" { self. odometer} 公里" )
def increment_odometer ( self, miles) :
self. odometer += miles
class Electrical ( Car) :
def __int__ ( self, make, model) :
super ( ) . __init__( make, model)
self. battery = 75
def getBat ( self) :
print ( self. battery)
my_elec = Electrical( 'tesla' , 'model“s' )
print ( my_elec. get_name)
my_elec. getBat( )
filename = 'pi_digits.txt'
with open ( filename) as file_object:
for line in file_object:
print ( line)
filename = 'pi_digits.txt'
with open ( filename) as file_object:
lines = file_object. readlines( )
pi_string = ''
for line in lines:
pi_string += line. rstrip( )
print ( pi_string)
print ( len ( pi_string) )
filename = 'programming.txt'
with open ( filename, 'w' ) as file_object:
file_object. write( "I love programming." )
file_object. write( "I love creating new games." )