検索

Python Day- Looping-Exercises and tasks

素数:
1 で割り切れる数とそれ自体を素数と呼びます。(例-->3,5,7)

1) 素数を見つけるかどうか:

no = int(input("Enter no. "))
div = 2
while div<no: if no print prime break div else:>



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

<pre class="brush:php;toolbar:false">1)Enter no. 5
  Prime
2)Enter no. 6
  Not Prime

2) 入力された数値を反転し、反転した数値が素数かどうかを調べます。

def reverse_a_no(no):
    reverse = 0
    while no>0:
        rem = no%10
        reverse = (reverse*10) + rem
        no//=10 #no=no//10
    return reverse

no = int(input("Enter no. ")) 
reversed_no = reverse_a_no(no) #31 71
print(reversed_no)
def find_prime(no):
    div = 2
    while div<no: if no return false break div else: true result1="find_prime(no)" result2="find_prime(reversed_no)" result2: print number emirp>



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

<pre class="brush:php;toolbar:false">1)Enter no. 15
  51
  EMIRP number
2)Enter no. 14
  41
  not EMIRP number

完全数
完全な数とは、割り切れる数の合計がその数字に等しいことを意味します。(例-->6 は 1,2,3 で割り切れ、1 2 3=6)

def find_perfect(no):
    total = 0
    div = 1
    while div<no: if no total="total" div else: no: return true false no. result="find_perfect(no)" true: print number perfect>



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

<pre class="brush:php;toolbar:false">Enter no. 6
Perfect Number

平方根:

入力数値の 2 乗とその平方根数値の桁の合計を求めます。

def square(no):
    return no**2

no=int(input("Enter the number:"))
result=square(no)

def sum_of_digits(num):
        sum=0
        while num>0:
            sum=sum+num%10
            num=num//10
        return sum

if result



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

<pre class="brush:php;toolbar:false">Enter the number:4
7

上記の例では、指定された入力番号は 4 です。
--> 4 の平方根は 4x4=16
--> その平方数の桁の合計 1 6=7.

タスク -1 **
**自動同型数

数字の四角形が同じ数字で終わるかどうかを確認します。
例: 5 → オートモーフィック (5²=25)、6 → オートモーフィック (6²=36)、7 → オートモーフィックではありません。

def square(no):
    return no**2

no=int(input("Enter the number:"))
result=square(no)
print(result)

while result>0:
    rem=result%10
    if rem==no:
        print("Automorphic number")
        break
    else:
        print("Not Automorphic number")
        break

出力:

1)Enter the number:5
  25
  Automorphic number
2)Enter the number:4
  16
  Not Automorphic number

タスク:2
フィボナッチ数列

指定された数値までのフィボナッチ数列を生成します。
例: 入力: 10 → 出力: 0、1、1、2、3、5、8。

no = int(input("Enter the number of required sequence: "))

first_num,sec_num =0 ,1

while first_num 



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

<pre class="brush:php;toolbar:false">Enter the number of required sequence: 10
0 1 1 2 3 5 8 

以上がPython Day - ループ - 演習とタスクの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

Numpyアレイで実行できる一般的な操作は何ですか?Numpyアレイで実行できる一般的な操作は何ですか?May 02, 2025 am 12:09 AM

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Pythonを使用したデータ分析では、配列はどのように使用されていますか?Pythonを使用したデータ分析では、配列はどのように使用されていますか?May 02, 2025 am 12:09 AM

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc

リストのメモリフットプリントは、Pythonの配列のメモリフットプリントとどのように比較されますか?リストのメモリフットプリントは、Pythonの配列のメモリフットプリントとどのように比較されますか?May 02, 2025 am 12:08 AM

listsandnumpyarraysinpythonhavedifferentmemoryfootprints:listsaremoreflexiblellessmemory-efficient、whileenumpyarraysaraysareoptimizedfornumericaldata.1)listsstorereferencesto objects、with whowedaround64byteson64-bitedatigu

実行可能なPythonスクリプトを展開するとき、環境固有の構成をどのように処理しますか?実行可能なPythonスクリプトを展開するとき、環境固有の構成をどのように処理しますか?May 02, 2025 am 12:07 AM

toensurepythonscriptsbehaveCorrectlyAcrossDevelosment、staging、and Production、usetheseStrategies:1)環境variablesforsimplestetings、2)configurationfilesforcomplexsetups、and3)dynamicloadingforadaptability.eachtododododododofersuniquebentandrequiresca

Pythonアレイをどのようにスライスしますか?Pythonアレイをどのようにスライスしますか?May 01, 2025 am 12:18 AM

Pythonリストスライスの基本的な構文はリストです[start:stop:step]。 1.STARTは最初の要素インデックス、2。ストップは除外された最初の要素インデックスであり、3.ステップは要素間のステップサイズを決定します。スライスは、データを抽出するためだけでなく、リストを変更および反転させるためにも使用されます。

どのような状況で、リストは配列よりもパフォーマンスが向上しますか?どのような状況で、リストは配列よりもパフォーマンスが向上しますか?May 01, 2025 am 12:06 AM

ListSoutPerformArraysIn:1)ダイナミシジョンアンドフレーケンティオン/削除、2)ストーリングヘテロゼンダタ、および3)メモリ効率の装飾、ButmayhaveslightPerformancostsinceNASOPERATIONS。

PythonアレイをPythonリストに変換するにはどうすればよいですか?PythonアレイをPythonリストに変換するにはどうすればよいですか?May 01, 2025 am 12:05 AM

toconvertapythonarraytoalist、usetheList()constructororageneratorexpression.1)importhearraymoduleandcreateanarray.2)useList(arr)または[xforxinarr] toconvertoalistは、largedatatessを変えることを伴うものです。

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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)