検索
ホームページバックエンド開発Python チュートリアルPython の Day-Loop - Range 関数とインデックス作成の使用、タスク

Python Day-Loop-Using Range Function and Indexing,Tasks

フィボナッチ数列:
1) 3 つの変数を使用する:

f, s = -1, 1
t = 0
while t



<p>出力:<br>
</p>

<pre class="brush:php;toolbar:false">0 1 1 2 3 5 8 13 21

2) 2 つの変数を使用する:

f, s = -1, 1 
while f+s



<p>出力:<br>
</p>

<pre class="brush:php;toolbar:false">0 1 1 2 3 5 8 13 

範囲関数:

range() 関数は、一連の数値を生成するために使用されます。これは、特定の回数を繰り返すためのループでよく使用されます。

構文:

範囲(スタート、ストップ、ステップ)

-->start (オプション): シーケンスの開始番号。指定しない場合、デフォルトは 0 です。

-->stop (必須): シーケンスが終了する番号 (排他的、つまり出力には含まれません)。

-->step (オプション): 増分値または減分値。指定しない場合、デフォルトは 1 です。

例:

print("First Output")
for no in range(10):
    print(no, end=' ')

print("\nSecond Output")
for no in range(1,10):
    print(no, end=' ')

print("\nThird Output")

for no in range(5,10):
    print(no, end=' ')

print("\nFourth Output")
for no in range(1,10,2):
    print(no, end=' ')

print("\nFifth Output")
for no in range(3,15,3):
    print(no, end=' ')

print("\nSixth Output")
for no in range(10,1):
    print(no, end=' ')

print("\nSeventh Output")
for no in range(10,1,-1):
    print(no, end=' ')

print("\nEighth Output")
for no in range(20,3,-1):
    print(no, end=' ')

print("\nNineth Output")
for no in range(20,2,-2):
    print(no, end=' ')

出力:

First Output
0 1 2 3 4 5 6 7 8 9 
Second Output
1 2 3 4 5 6 7 8 9 
Third Output
5 6 7 8 9 
Fourth Output
1 3 5 7 9 
Fifth Output
3 6 9 12 
Sixth Output

Seventh Output
10 9 8 7 6 5 4 3 2 
Eighth Output
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 
Nineth Output
20 18 16 14 12 10 8 6 4

6 番目の出力の説明:

range() 関数には、シーケンスを逆に生成するためのステップ パラメーターが必要です。ステップが指定されていない場合、デフォルトとして 1 が使用されます。これは、シーケンスが 10 から 1 に増分しようとすることを意味しますが、10 は 1 より大きいため、数値は生成されません。

ネガティブインデックス:
通常、インデックス付けは 0 から開始されますが、-1 から開始することもできます。これは、負のインデックス付け (-1 から開始) を意味します。

例:

name = 'ABCDEFGHI'

for letter in name[0:5]:  
    print(letter, end=' ')
print()
for letter in name[0:6:2]:
    print(letter, end=' ')
print()
for letter in name[8:0:-1]:
    print(letter, end=' ')
print()
for letter in name[8:2:-1]:
    print(letter, end=' ')
print()
for letter in name[8:-1:-1]:
    print(letter, end=' ')
print()
for letter in name[8:3:-2]:
    print(letter, end=' ')
print()
for letter in name[8::-1]:
    print(letter, end=' ')
print()
for letter in name[::]:
    print(letter, end=' ')
print()
for letter in name[6::]:
    print(letter, end=' ')
print()
for letter in name[2::2]:
    print(letter, end=' ')

出力:

A B C D E 
A C E 
I H G F E D C B 
I H G F E D 

I G E 
I H G F E D C B A 
A B C D E F G H I 
G H I 
C E G I 

説明:5 番目の出力()
名前[8:-1:-1]
このインデックス付けでは、start は 8 で、上記の例では最後の値ですが、end -1 は最後の値も示しているため、出力は何も返しません。

指定された文字列の回文を検索するかどうか:

name = input("Enter word: ")
if name[::] == name[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

出力:

Enter word: amma
Palindrome

パターン形成:
例:1

for num in range(1,6):
    print("* " * num)

出力:

* 
* * 
* * * 
* * * * 
* * * * * 

例:2

for num in range(5,0,-1):
    print("* " * num)

出力:

* * * * * 
* * * * 
* * * 
* * 
* 

注: * は 2 つの文字列間では機能しますが、2 つの文字列間では機能しません。(例: a*2-->aa、a 2-->a2)

例:3

digit = "1"
for num in range(5,0,-1): 
    print(digit * num)
    digit = str(int(digit)+1) 
print()

出力:

11111
2222
333
44
5

タスク:
word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

1)ABCDEFGHI
2)XYZ
3)ZYXWV
4)アセギ
5)イゲカ
6)ZXVTRPNLJHFDB

word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print("First Output")
for letter in word[0:9]:
    print(letter , end=" ")

print("\nSecond Output")
for letter in word[23::]:
    print(letter , end=" ")

print("\nThird Output")
for letter in word[-1:-6:-1]:
    print(letter , end=" ")

print("\nFouth Output")
for letter in word[0:9:2]:
    print(letter , end=" ")

print("\nFifth Output")
for letter in word[8::-2]:
    print(letter , end=" ")

print("\nSixth Output")
for letter in word[-1::-2]:
    print(letter , end=" ")

出力:

First Output
A B C D E F G H I 
Second Output
X Y Z 
Third Output
Z Y X W V 
Fouth Output
A C E G I 
Fifth Output
I G E C A 
Sixth Output
Z X V T R P N L J H F D B

以上がPython の Day-Loop - Range 関数とインデックス作成の使用、タスクの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python:コンパイラまたはインタープリター?Python:コンパイラまたはインタープリター?May 13, 2025 am 12:10 AM

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

ループvs whileループ用のpython:いつ使用するか?ループvs whileループ用のpython:いつ使用するか?May 13, 2025 am 12:07 AM

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

Pythonループ:最も一般的なエラーPythonループ:最も一般的なエラーMay 13, 2025 am 12:07 AM

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

ループの場合、およびPythonのループ:それぞれの利点は何ですか?ループの場合、およびPythonのループ:それぞれの利点は何ですか?May 13, 2025 am 12:01 AM

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

Python:編集と解釈に深く掘り下げますPython:編集と解釈に深く掘り下げますMay 12, 2025 am 12:14 AM

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?May 12, 2025 am 12:09 AM

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

ループ対pythonのループの場合:説明されたキーの違いループ対pythonのループの場合:説明されたキーの違いMay 12, 2025 am 12:08 AM

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

ループのために:実用的なガイドループのために:実用的なガイドMay 12, 2025 am 12:07 AM

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境