jupyter: jupytext: formats: ipynb,md text_representation: extension: .md format_name: markdown format_version: ‘1.2’ jupytext_version: 1.3.2 kernelspec: display_name: Python 3 language: python name: python3
第一章 起步
print ( "hello world" )
重启kernel点击kernel restart
第二章 变量和简单的数据类型
message = "Hello world"
print ( message)
message = 'abc'
print ( message)
name = "ada lovelace"
print ( name. title( ) )
name = "Ada"
print ( name. upper( ) )
print ( name. lower( ) )
first_name = 'ada'
last_name = 'lovelace'
full_name = first_name+ ' ' + last_name
message = 'Hello, ' + full_name. title( ) + '!'
print ( message)
print ( "\tPython" )
print ( "language:\n\tpython\n\tC\n\tjava" )
language = " \tpython \t"
print ( language)
print ( language. rstrip( ) )
language= language. rstrip( )
print ( language)
language = "\tpython\t"
print ( language)
print ( language. lstrip( ) )
language = "\tpython\t"
print ( language)
print ( language. strip( ) )
language = "\tpy thon\t"
print ( language)
print ( language. strip( ) )
0.2 + 0.1
age = 34
message = "happy " + str ( age) + 'th birthday'
print ( message)
print ( 3 + 5 )
print ( 10 - 2 )
print ( 2 * 4 )
print ( int ( 16 / 2 ) )
import this
第三章 列表简介
bicycle = [ 'trek' , 'cannondale' , 'redline' , 'specialized' ]
print ( bicycle)
print ( bicycle[ 0 ] . title( ) )
print ( bicycle[ - 1 ] )
bicycle[ 0 ] = 'ducati'
print ( bicycle)
bicycle. append( 'honda' )
print ( bicycle)
bicycle. insert( 0 , 'abc' )
print ( bicycle)
motorcycles = [ 'honda' , 'yamaha' , 'suzuki' ]
print ( motorcycles)
del motorcycles[ 0 ]
print ( motorcycles)
motorcycles = [ 'honda' , 'yamaha' , 'suzuki' ]
print ( motorcycles)
del motorcycles[ 1 ]
print ( motorcycles)
motorcycles = [ 'honda' , 'yamaha' , 'suzuki' ]
print ( motorcycles)
poppedmo = motorcycles. pop( )
print ( motorcycles)
print ( poppedmo)
motorcycles = [ 'honda' , 'yamaha' , 'suzuki' ]
print ( motorcycles)
poppedmo = motorcycles. pop( 0 )
print ( motorcycles)
print ( poppedmo)
motorcycles = [ 'honda' , 'yamaha' , 'suzuki' ]
print ( motorcycles)
motorcycles. remove( 'honda' )
print ( motorcycles)
cars = [ 'bmw' , 'audi' , 'toyota' , 'subaru' ]
cars. sort( )
print ( cars)
cars = [ 'bmw' , 'audi' , 'toyota' , 'subaru' ]
cars. sort( reverse= True )
print ( cars)
cars = [ 'bmw' , 'audi' , 'toyota' , 'subaru' ]
print ( sorted ( cars) )
print ( cars)
cars = [ 'bmw' , 'audi' , 'toyota' , 'subaru' ]
cars. reverse( )
print ( cars)
cars = [ 'bmw' , 'audi' , 'toyota' , 'subaru' ]
len ( cars)
第四章 操作列表
magicians = [ 'Alice' , 'david' , 'carolina' ]
for magician in magicians:
print ( magician)
for value in range ( 1 , 5 ) :
print ( value)
numbers = list ( range ( 2 , 11 , 2 ) )
print ( numbers)
squares = [ ]
for value in range ( 1 , 11 ) :
square = value** 2
squares. append( square)
print ( squares)
digits = list ( range ( 10 ) )
print ( digits)
print ( min ( digits) )
print ( max ( digits) )
print ( sum ( digits) )
squares = [ value** 2 for value in range ( 1 , 11 ) ]
print ( squares)
players = [ 'charles' , 'martina' , 'michael' , 'florence' , 'eli' ]
print ( players[ 0 : 3 ] )
for i in players[ : 3 ] :
print ( i. title( ) )
my_food = [ 'pizza' , 'falafel' , 'carrot cake' ]
friend_food = my_food[ : ]
print ( my_food)
print ( friend_food)
my_food. append( 'cannoli' )
friend_food. append( 'ice cream' )
print ( my_food)
print ( friend_food)
my_food = [ 'pizza' , 'falafel' , 'carrot cake' ]
friend_food = my_food
my_food. append( 'cannoli' )
friend_food. append( 'ice cream' )
print ( my_food)
print ( friend_food)
dimensions = ( 200 , 5 )
print ( dimensions[ 0 ] )
print ( dimensions[ 1 ] )
for i in dimensions:
print ( i)
dimensions = ( 200 , 5 )
for i in dimensions:
print ( i)
dimensions = ( 1200 , 15 )
for i in dimensions:
print ( i)
第五章 if 语句
cars = [ 'audi' , 'bmw' , 'subaru' , 'toyota' ]
for car in cars:
if car == 'bmw' :
print ( car. upper( ) )
else :
print ( car. title( ) )
car = 'audi'
car == 'Audi'
food = 'mushrooms'
if food != 'pizza' :
print ( 'no' )
age_0 = 11
age_1 = 20
age_0 < 1 and age_1 > 5
age_0 = 11
age_1 = 20
age_0 > 1 and age_1 > 5
age_0 = 11
age_1 = 20
age_0 < 1 or age_1 > 5
age_0 = 11
age_1 = 20
age_0 < 1 or age_1 < 5
'a' in 'assxsxsx'
request = [ 'mushrooms' , 'onions' , 'pineapple' ]
print ( 'mushrooms' in request)
print ( 'abc' in request)
banned_users = [ 'andrew' , 'carolina' , 'david' ]
user = 'marie'
if user not in banned_users:
print ( user. title( ) , 'you can post a response if you wish' )
age = 17
if age >= 18 :
print ( "you are old enough to vote" )
print ( "Have you registered to vote yet?" )
else :
print ( 'Sorry,you are too young to vote.' )
age = 12
if age < 4 :
print ( 'Your admission cost is $0' )
elif age< 18 :
print ( 'Your admission cost is $5' )
else :
print ( 'Your admission cost is $10' )
age = 12
if age < 4 :
price = 0
elif age< 18 :
price = 5
else :
price = 10
print ( "Your admission cost is $%s" % str ( price) )
age = 12
if age < 4 :
price = 0
elif age < 18 :
price = 5
elif age < 65 :
price = 10
else :
price = 5
print ( "Your admission cost is $%s" % str ( price) )
request = [ 'mushrooms' , 'extra cheese' ]
if 'mushrooms' in request:
print ( 'adding mushrooms' )
if 'abd' in request:
print ( 'add abd' )
if 'extra cheese' in request:
print ( 'add cheese' )
request = [ 'mushrooms' , 'extra cheese' ]
if 'mushrooms' in request:
print ( 'adding mushrooms' )
elif 'abd' in request:
print ( 'add abd' )
elif 'extra cheese' in request:
print ( 'add cheese' )
request = [ 'mushrooms' , 'extra cheese' ]
for i in request:
if i == 'mushrooms' :
print ( 'sorry,we are out of mushrooms ringh now.' )
else :
print ( 'adding' , i)
request = [ ]
if request:
for i in request:
print ( 'adding' , i)
else :
print ( "Are you sure you want a plain pizza" )
available = [ 'mushrooms' , 'olives' , 'green peppers' , 'extra cheese' ]
request = [ 'mushrooms' , 'french fires' ]
for i in request:
if i in available:
print ( 'adding' , i)
else :
print ( 'sorry,we do not have' , i)
第6章 字典
alien = { 'color' : 'green' }
del alien[ 'color' ]
alien
a_num = { 1 : 1 , 2 : 2 , 3 : 5 , 4 : 8 }
for i, j in a_num. items( ) :
print ( i* j)
for i in a_num. keys( ) :
print ( i)
for i in a_num. values( ) :
print ( i)
第7章 用户输入和while循环
a1 = input ( 'num' )
print ( type ( a1) )
active = True
while active:
message = input ( 'repeat' )
if message == 'quit' :
active = False
else :
print ( message)
unconfirmed_users = [ 'alice' , 'brian' , 'candace' ]
confirmed_users = [ ]
while unconfirmed_users:
current_user = unconfirmed_users. pop( )
print ( 'verifying: ' + current_user)
confirmed_users. append( current_user)
pets = [ 'dog' , 'cat' , 'dog' , 'goldfish' , 'cat' , 'rabbit' , 'cat' ]
print ( pets)
while 'cat' in pets:
pets. remove( 'cat' )
print ( pets)