日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python集合例题_python练习题集合-2

發(fā)布時(shí)間:2024/9/19 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python集合例题_python练习题集合-2 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

author:headsen chen

date:2018-06-01?15:39:26

習(xí)題17,文件的更多操作

[root@localhost py]# echo 1234567890 >cc.txt

[root@localhost py]# cat file3.py

#!/usr/bin/env pythonfromsys import argvfromos.path import exists

script,from_file,to_file=argv

print"Copying from %s to %s" %(from_file,to_file)

# we coulddo these two on one line too,how?input=open(from_file)

indata=input.read()

print"The input file is %d bytes long"%len(indata)

print"Does the output file exist? %r"%exists(to_file)

print"Ready,hit RETURN to continue, CTRL-C to abort."raw_input()

output= open(to_file,'w')

output.write(indata)

print"Alright,all done."output.close()

input.close()

[root@localhost py]#python file3.py cc.txt dd.txt

Copying fromcc.txt to dd.txt

The input fileis 11bytes long

Does the output file exist? True

Ready,hit RETURN tocontinue, CTRL-C to abort.

Alright,all done.

檢驗(yàn)新生成的dd.txt文件:

[root@localhost py]# cat dd.txt

1234567890

這里不過dd.txt是否存在,若存在:清空然后復(fù)制cc.txt的內(nèi)容過來,若不存在dd.txt文件就創(chuàng)建dd.txt文件并復(fù)制內(nèi)容過來

補(bǔ)充:exists這個(gè)命令將文件名字符串作為參數(shù),如果文件存在的話,它將返回 True,否則將返回 False。

習(xí)題18:函數(shù) ---> 理解成“迷你腳本”

函數(shù)可以做三樣事情:

1.它們給代碼片段命名,就跟“變量”給字符串和數(shù)字命名一樣。

2.它們可以接受參數(shù),就跟你的腳本接受 argv 一樣。

3.通過使用 #1 和 #2,它們可以讓你創(chuàng)建“微型腳本”或者“小命令”。

[root@localhost py]#cat func1.py#!/usr/bin/env python

#this is like you scripts with argv

def print_two(*args):

arg1,arg2=argsprint "arg1:%r,arg2:%r" %(arg1,arg2)#ok,that *args is actually pointless,we can just do this

defprint_two_again(arg1,arg2):print "arg1:%r,arg2:%r" %(arg1,arg2)#this just takes one argument

defprint_one(arg1):print "arg1:%r"%arg1#this one takes no arguments

defprint_none():print "I got nothing."print_two('zen','hello')

print_two_again('ZEN','HELLO')

print_one('First!')

print_none()

[root@localhost py]#python func1.py

arg1:'zen',arg2:'hello'arg1:'ZEN',arg2:'HELLO'arg1:'First!'I got nothing.

習(xí)題 19: 函數(shù)和變量

[root@localhost py]#cat func2.py#!/usr/bin/env python

defcheese_and_crackers(cheese_count, boxes_of_crackers):print "You have %d cheeses!" %cheese_countprint "You have %d boxes of crackers!" %boxes_of_crackersprint "Man that's enough for a party!"

print "Get a blanket.\n"

print "We can just give the function numbers directly:"cheese_and_crackers(20, 30)print "OR, we can use variables from our script:"amount_of_cheese= 10amount_of_crackers= 50cheese_and_crackers(amount_of_cheese, amount_of_crackers)print "We can even do math inside too:"cheese_and_crackers(10 + 20, 5 + 6)print "And we can combine the two, variables and math:"cheese_and_crackers(amount_of_cheese+ 100, amount_of_crackers + 1000)

[root@localhost py]#python func2.py

We can just give the function numbers directly:

You have20cheeses!

You have30boxes of crackers!

Man that's enough for a party!

Get a blanket.

OR, we can use variablesfromour script:

You have10cheeses!

You have50boxes of crackers!

Man that's enough for a party!

Get a blanket.

We can even do math inside too:

You have30cheeses!

You have11boxes of crackers!

Man that's enough for a party!

Get a blanket.

And we can combine the two, variablesandmath:

You have110cheeses!

You have1050boxes of crackers!

Man that's enough for a party!

Get a blanket.

習(xí)題 20: 函數(shù)和文件

[root@localhost py]#cat aa.txt

aaaaaa

bbbbbb

cccccc

[root@localhost py]#cat func-file.py

#!/usr/bin/env python

from sys importargv

script, input_file=argvdefprint_all(f):printf.read()defrewind(f):

f.seek(0)defprint_a_line(line_count, f):printline_count, f.readline()

current_file=open(input_file)print "First let's print the whole file:\n"print_all(current_file)print "Now let's rewind, kind of like a tape."rewind(current_file)print "Let's print three lines:"current_line= 1print_a_line(current_line, current_file)

current_line= current_line + 1print_a_line(current_line, current_file)

current_line= current_line + 1print_a_line(current_line, current_file)

[root@localhost py]#python func-file.py aa.txt

First let's print the whole file:

aaaaaa

bbbbbb

cccccc

Now let's rewind, kind of like a tape.

Let's print three lines:

1aaaaaa2bbbbbb3 cccccc

習(xí)題 21: 函數(shù)可以返回東西

[root@localhost py]#cat func-return.py#!/usr/bin/env python#-*- coding:utf-8 -*-

defadd(a, b):print "ADDING %d + %d" %(a, b)return a +bdefsubtract(a, b):print "SUBTRACTING %d - %d" %(a, b)return a -bdefmultiply(a, b):print "MULTIPLYING %d * %d" %(a, b)return a *bdefdivide(a, b):print "DIVIDING %d / %d" %(a, b)return a /bprint "Let's do some math with just functions!"age= add(30, 5)

height= subtract(78, 4)

weight= multiply(90, 2)

iq= divide(100, 2)print "Age: %d, Height: %d, Weight: %d, IQ: %d" %(age, height,weight, iq)#A puzzle for the extra credit, type it in anyway.# pullzle:智力題

print "Here is a puzzle."what= add(age, subtract(height, multiply(weight, divide(iq,2))))print "That becomes:", what, "Can you do it by hand?"

[root@localhost py]#python func-return.py

Let's do some math with just functions!

ADDING 30 + 5SUBTRACTING78 - 4MULTIPLYING90 * 2DIVIDING100 / 2Age:35, Height: 74, Weight: 180, IQ: 50Hereisa puzzle.

DIVIDING50 / 2MULTIPLYING180 * 25SUBTRACTING74 - 4500ADDING35 + -4426That becomes:-4391 Can you do it by hand?

習(xí)題 24: 練習(xí)

[root@localhost py]#cat practise.py#!/usr/bin/env python

print "Let's practice everything."

print 'You\'d need to know \'bout escapes with \\ that do \nnewlines and \t tabs.'poem= """\tThe lovely world

with logic so firmly planted

cannot discern \n the needs of love

nor comprehend passion from intuition

and requires an explanation

\n\t\twhere there is none."""

print "--------------"

printpoemprint "--------------"five= 10 - 2 + 3 - 6

print "This should be five: %s" %fivedefsecret_formula(started):

jelly_beans= started * 500jars= jelly_beans / 1000crates= jars / 100

returnjelly_beans, jars, crates

start_point= 10000beans, jars, crates=secret_formula(start_point)print "With a starting point of: %d" %start_pointprint "We'd have %d beans, %d jars, and %d crates." %(beans,jars, crates)

start_point= start_point / 10

print "We can also do that this way:"

print "We'd have %d beans, %d jars, and %d crates." %secret_formula(start_point)

[root@localhost py]#python practise.py

Let's practice everything.

You'd need to know'bout escapes with \ that do

newlinesandtabs.--------------The lovely world

with logic so firmly planted

cannot discern

the needs of love

nor comprehend passionfromintuitionandrequires an explanation

where thereisnone.--------------This should be five:5With a starting point of:10000We'd have 5000000 beans, 5000 jars, and 50 crates.

We can also do that this way:

We'd have 500000 beans, 500 jars, and 5 crates.

習(xí)題 25: 更多的練習(xí)

[root@localhost py]#cat practise2.py#!/usr/bin/env python

defbreak_words(stuff):"""This function will break up words for us."""words= stuff.split('')returnwordsdefsort_words(words):"""Sorts the words."""

returnsorted(words)defprint_first_word(words):"""Prints the first word after popping it off."""word=words.pop(0)printworddefprint_last_word(words):"""Prints the last word after popping it off."""word= words.pop(-1)printworddefsort_sentence(sentence):"""Takes in a full sentence and returns the sorted words."""words=break_words(sentence)returnsort_words(words)defprint_first_and_last(sentence):"""Prints the first and last words of the sentence."""words=break_words(sentence)

print_first_word(words)

print_last_word(words)defprint_first_and_last_sorted(sentence):"""Sorts the words then prints the first and last one."""words=sort_sentence(sentence)

print_first_word(words)

print_last_word(words)

習(xí)題26:邏輯術(shù)語

在 python 中我們會(huì)用到下面的術(shù)語(字符或者詞匯)來定義事物的真(True)或者假(False)。計(jì)算機(jī)的邏輯就是在程序的某個(gè)位置檢查這些字符或者變量組合

在一起表達(dá)的結(jié)果是真是假。

? and 與

? or 或

? not 非

? != (not equal) 不等于

? == (equal) 等于

? >= (greater-than-equal) 大于等于

? <= (less-than-equal) 小于等于

? True 真

? False 假

真值表

我們將使用這些字符來創(chuàng)建你需要記住的真值表。

not False True

not True False

True or FalseTrue

True or True True

False or True True

False or False False

True and FalseFalse

True and True True

False and True False

False and False False

not (True or False) False

not (True or True) False

not (False or True) False

not (False or False) True

not (True and False) True

not (True and True) False

not (False and True) True

not (False and False) True

1 != 0 True

1 != 1 False

0 != 1 True

0 != 0 False

1 == 0 False

1 == 1 True

0 == 1 False

0 == 0 True

習(xí)題27:bool 值運(yùn)算

[root@localhost py]# cat bool.py

#!/usr/bin/env python

print True and True

print False and True

print 1 == 1 and 2 == 1

print "test" == "test"

print 1 == 1 or 2 != 1

print True and 1 == 1

print False and 0 != 0

print True or 1 == 1

print "test" == "testing"

print 1 != 0 and 2 == 1

print "test" != "testing"

print "test" == 1

print not (True and False)

print not (1 == 1 and 0 != 1)

print not (10 == 1 or 1000 == 1000)

print not (1 != 10 or 3 == 4)

print not ("testing" == "testing" and "Zed" == "Cool Guy")

print 1 == 1 and not ("testing" == 1 or 1 == 0)

print "chunky" == "bacon" and not (3 == 4 or 3 == 3)

print 3 == 3 and not ("testing" == "testing" or "Python" == "Fun")

[root@localhost py]# python bool.py

True

False

False

True

True

True

False

True

False

False

True

False

True

False

False

False

True

True

False

False

習(xí)題28:bool運(yùn)算

3 != 4 and not ("testing" != "test" or "Python" == "Python")

接下來你將看到這個(gè)復(fù)雜表達(dá)式是如何逐級(jí)解為一個(gè)單獨(dú)結(jié)果的:1. 解出每一個(gè)等值判斷:

a.3 != 4 為 True : True and not ("testing" != "test" or "P

ython"=="Python")

b. "testing" != "test" 為 True : True and not (True or "Pyt

hon"=="Python")

c. "Python" == "Python" : True and not (True orTrue)2. 找到括號(hào)中的每一個(gè) and/or:

a. (Trueor True) 為 True: True and not(True)3. 找到每一個(gè) not并將其逆轉(zhuǎn):

a.not (True) 為 False: True andFalse4. 找到剩下的 and/or,解出它們的值:

a. TrueandFalse 為 False

這樣我們就解出了它最終的值為 False.

View Code

習(xí)題 29: 如果(if)

[root@localhost py]#cat if.py#!/usr/bin/env python

people = 20cats= 30dogs= 15

if people

if people >cats:print "Not many cats! The world is saved!"

if people

if people >dogs:print "The world is dry!"dogs+= 5

if people >=dogs:print "People are greater than or equal to dogs."

if people <=dogs:print "People are less than or equal to dogs."

if people ==dogs:print "People are dogs."

View Code

[root@localhost py]#python if.py

Too many cats! The world isdoomed!

The worldisdry!

People are greater thanorequal to dogs.

People are less thanorequal to dogs.

People are dogs.

View Code

習(xí)題30:if -elase 結(jié)合使用

[root@localhost py]#cat if.py#!/usr/bin/env python

people = 20cats= 30dogs= 15

if people

if people >cats:print "Not many cats! The world is saved!"

if people

if people >dogs:print "The world is dry!"dogs+= 5

if people >=dogs:print "People are greater than or equal to dogs."

if people <=dogs:print "People are less than or equal to dogs."

if people ==dogs:print "People are dogs."

View Code

[root@localhost py]# python if-else.py

We should take the cars.

Maybe we could take the buses.

Alright, let's just take the buses.

總結(jié)

以上是生活随笔為你收集整理的python集合例题_python练习题集合-2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品欧美激情 | 999精品在线视频 | 久久精品在线播放 | 亚洲综合视频一区 | 91成人精品一区在线播放 | 免费国产a级片 | 国产女人在线视频 | 国产视频日韩 | 国产午夜精品久久久 | 日韩毛片在线视频 | av卡一卡二 | 91丨国产丨白丝 | 污污内射在线观看一区二区少妇 | 97免费在线观看视频 | 青娱乐超碰 | 国产亚洲精品久久久久四川人 | 国产又白又嫩又爽又黄 | 在线看免费av | 日韩精品片 | 人妻无码一区二区三区久久99 | 灌篮高手全国大赛电影 | 欧美日p视频 | 啪啪网页 | 欧美激情在线播放 | 日本一区二区三区欧美 | 日本v片| 亚洲AV永久无码国产精品国产 | 国产精品国产精品国产专区 | 91精品国产欧美一区二区 | 麻豆视频免费在线观看 | 欧美少妇xxx | 色久综合 | 虫族全黄h全肉污文 | 可以在线观看av的网站 | 欧美性xxxx在线播放 | 日韩高清一二三区 | 亚洲一区二区三区四区在线观看 | 九九视屏| 女人床技48动态图 | 欧美激情二区三区 | 超级碰碰97 | 欧美成人三级在线播放 | 亚洲国产婷婷香蕉久久久久久99 | 红桃视频亚洲 | 亚洲精品在线不卡 | aaa黄色片| 欧美熟妇7777一区二区 | 手机在线观看日韩av | 亚洲gay视频| 成人亚洲综合 | 春色伊人 | 亚洲最大网站 | av大西瓜 | 老湿机69福利区午夜x片 | 裸体女视频 | 91成人在线免费视频 | 伊人久久网站 | 欧美精品一区二区久久婷婷 | 精品久久久久久中文字幕人妻最新 | 色偷偷噜噜噜亚洲男人的天堂 | 7777奇米影视 | 免费一级a毛片夜夜看 | 日韩精品在线第一页 | 欧美色视频在线观看 | 麻豆av一区二区三区 | 亚洲色偷精品一区二区三区 | 被灌满精子的波多野结衣 | 天堂网在线资源 | 熟女少妇在线视频播放 | 免费精品在线视频 | 在线免费观看视频a | 免费成人美女在线观看 | 在线欧美a| 亚洲av永久无码精品三区在线 | 91久久精品国产91久久性色tv | 免费麻豆国产一区二区三区四区 | 国产真实在线 | 夜夜操狠狠干 | 日本成人在线不卡 | 国产丝袜美女 | 黄色网址www | 无码成人精品区一级毛片 | 用力抵着尿进去了h | 疯狂做爰的爽文多肉小说王爷 | 成人无码视频 | いいなり北条麻妃av101 | 天天色天天综合 | 在线视频精品免费 | 2018天天干天天操 | 国产亚洲福利 | 亚洲欧美国产高清va在线播放 | 美女作爱网站 | 少妇精品无码一区二区三区 | 青青草社区视频 | www.99热| 日韩骚片 | 中文字幕一区二区三区乱码 | 日本免费精品视频 | 免费观看成人 |