1) 文字列の間にスペースを追加する
txt = "TodayIsFriday" #Today is Friday first = True for letter in txt: if letter>='A' and letter <p>出力:<br> 今日は金曜日です</p> <p><strong>2)文字列間のスペースを削除するには</strong><br> </p> <pre class="brush:php;toolbar:false">txt = " Today Is Friday" #Today is Friday for letter in txt: if letter==' ': pass else: print(letter,end='')
出力:
今日は金曜日
3) ltrim- 文字列の左側のスペースを削除します。
#ltrim txt = " Today Is Friday" #Today is Friday alphabet = False for letter in txt: if letter==' ' and alphabet == False: pass else: alphabet = True print(letter,end='')
4) rtrim - 文字列の右側のスペースを削除します。
txt = "Today Is Friday " #Today is Friday alphabet = False i = len(txt)-1 while i>=0: letter = txt[i] if letter==' ' and alphabet == False: pass else: alphabet = True end = i j = 0 while j <p>出力:</p> <p>今日は金曜日です</p> <p><strong>5) 指定された文字列から不要なスペースを削除します</strong><br> </p> <pre class="brush:php;toolbar:false">txt = "Today Is Friday" #Today is Friday i = 0 while i<len if txt print else: i> <p>出力:</p> <p>今日は金曜日です</p> <p><strong>再帰:</strong><br> 自分自身を呼び出す関数。</p> <p>ループ-->反復アプローチ。<br> 再帰 --> 再帰的アプローチ。</p> <p>例:1<br> </p> <pre class="brush:php;toolbar:false">def display(no): print(no, end=' ') no+=1 if no <p>出力:<br> </p> <pre class="brush:php;toolbar:false">1 2 3 4 5
階乗を呼び出すための再帰関数:
5!=5x4x3x2x1 (または) 5x4!
4!=4x3x2x1 (または) 4x3!
3!=3x2x1 (または) 3x2!
2!=2x1 (または) 2x1!
1!=1
例:2
def find_fact(no): if no==1: return 1 return no * find_fact(no-1) result = find_fact(5) print(result)
出力:
120
説明:
1) find_fact(5)
戻り値 5 * find_fact(4) #no-1 = 5-1 -->4
2) find_fact(4)
戻り値 4 * find_fact(3) #no-1 = 4-1 -->3
3) find_fact(3)
戻り値 3 * find_fact(2) #no-1 = 3-1 -->2
4) find_fact(2)
戻り値 2 * find_fact(1) #no-1 = 2-1 -->1
5) find_fact(1)
基本ケース: 1
基本ケース: 再帰の基本ケースは、再帰呼び出しを停止する条件です。
タスク:
strip() - 文字列の先頭と末尾からすべての空白文字 (スペース、タブ、改行) を削除します。
1) 指定された文字列の前後にある不要なスペースを削除します。
txt = " Today Is Friday " start = 0 end = len(txt) - 1 while start = 0: i = start while i = 0 and txt[j] == ' ': j -= 1 end = j break result = txt[start:end+1] print(result)
出力:
Today Is Friday
2) 再帰関数を使用して数値を反転する:
def reverse_a_no(no,reverse = 0): if no==0: return reverse rem = no%10 reverse = (reverse*10) + rem no=no//10 return reverse_a_no(no,reverse) no = int(input("Enter no. ")) reversed_no = reverse_a_no(no) print(reversed_no)
出力:
Enter no. 15 51
3)素数を見つけるかどうか:
def find_prime(no,div=2): if div<no: if no return false div find_prime else: true the number: print number emirp> <p>出力:<br> </p> <pre class="brush:php;toolbar:false">1) Enter the number: 11 EMIRP number 2) Enter the number: 15 not EMIRP number
4) フィボナッチを検索します:
def find_fibonacci(first_num,sec_num,no): if first_num > no: return print(first_num, end=" ") find_fibonacci(sec_num,first_num+sec_num,no) no = int(input("Enter the number: ")) find_fibonacci(0,1,no)
出力:
Enter the number: 10 0 1 1 2 3 5 8
5.回文を見つけるかどうか:
def palindrome(num,count=0): if num == 0: return count return palindrome(num//10,count*10+num%10) num=int(input("Enter the number:")) result=palindrome(num) if result==num: print("Palindrome") else: print("Not palindrome")
出力:
Enter the number:121 Palindrome
HackerRank アカウントを作成しました: https://www.hackerrank.com/dashboard
以上がループ、再帰、タスクを使用した Python Day-String 関数ロジックの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

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

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

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

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

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

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


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター
