مشخصات مقاله
-
2469
-
5.0
-
7708
-
1
-
0
آموزش ساختارهای کنترلی در پایتون
در زبان برنامهنویسی پایتون، از دستورات شرطی if-else برای انجام تصمیمگیریها استفاده میشود. تصمیمگیری، یکی از مهم ترین بخش ها در تمام زبانهای برنامهنویسی است. همانطور که نامش نشان میدهد، تصمیمگیری به ما اجازه میدهد که یک بلوک خاص از کد را برای یک تصمیم خاص اجرا کنیم. در اینجا، تصمیمگیریها براساس صحت شرایط خاصی انجام میشوند. بررسی این شرایط، اساس تصمیمگیریها است.
در پایتون، تصمیمگیری با استفاده از دستورات زیر انجام میشود:تو رفتگی در پایتون
به منظور تسهیل برنامهنویسی و سادگی، پایتون از پرانتز برای سطح بلوک کد استفاده نمیکند. در پایتون، تورفتگی (indentation) برای تعریف یک بلوک کد استفاده میشود. اگر دو عبارت در همان سطح تورفتگی باشند، به عنوان یک بلوک محسوب میشوند.
به طور عمومی، از "چهار فضای خالی" به عنوان تورفتگی استفاده میشود که مقدار معمولی تورفتگی در پایتون است. این استاندارد تورفتگی به برنامهنویسان کمک میکند تا کد خود را خواناتر و قابل فهمتر کنند و بلوکهای مختلف کد را تشخیص دهند. به عبارت دیگر، تورفتگی در پایتون برای تعیین ساختار بلوکهای کد و کنترل جریان برنامه بسیار مهم است.
دستور if در پایتون
دستور if برای آزمایش یک شرط خاص استفاده میشود و اگر شرط صحیح باشد، بلوکی از کد به نام بلوک if اجرا میشود. شرط دستور if میتواند هر عبارت منطقی معتبری باشد که به یکی از دو مقدار صحیح یا غلط تبدیل شود. تورفتگی به ما این امکان را میدهد که محدوده بلوک if را مشخص کنیم. توجه کنید که همه عباراتی که با یک تورفتگی یکسان نوشته شدهاند، به عنوان بلوک if تلقی میشوند. اگر شرط دستور if صحیح باشد، بلوک if اجرا میشود.
ساختار دستور if به صورت زیر است:
if expression:
statement
مثال
# Simple Python program to understand the if statement
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
خروجی
enter the number: 10 The Given number is an even numberمثال 2
در این برنامه، سه عدد را از کاربر گرفته، و آن ها را برای تشخیص بزرگ ترین عدد مقایسه و سپس چاپ میکنیم.
# Simple Python Program to print the largest of the three numbers.
a = int (input("Enter a: "));
b = int (input("Enter b: "));
c = int (input("Enter c: "));
if a>b and a>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given a is largest");
if b>a and b>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given b is largest");
if c>a and c>b:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given c is largest");
خروجی
Enter a: 100 Enter b: 120 Enter c: 130 From the above three numbers given c is largest
دستور if-else در پایتون
دستور if-else یک بلوک else را در کنار دستور if ارائه میدهد، اگر شرط صحیح باشد، بلوک if اجرا میشود. در غیر این صورت، بلوک else اجرا میشود.
ساختار دستور if-else به صورت زیر است:
if condition:
#block of statements
else:
#another block of statements (else-block)
مثال 1
این برنامه با گرفتن سن کاربر، آن را بررسی کرده و اگر سن کاربر زیر 18 سال باشد اجازه رای دادن را به او نمی دهد.
# Simple Python Program to check whether a person is eligible to vote or not.
age = int (input("Enter your age: "))
# Here, we are taking an integer num and taking input dynamically
if age>=18:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
خروجی
Enter your age: 90 You are eligible to vote !!مثال 2
برنامهای برای بررسی زوج و فرد بودن یک عدد
# Simple Python Program to check whether a number is even or not.
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
else:
print("The Given Number is an odd number")
خروجی
enter the number: 10 The Given number is even number
دستور elif در پایتون
دستور elif به ما این امکان را میدهد تا چندین شرط را بررسی کرده و بر اساس شرطی که صحیح باشد، بلوک خاصی از عبارات را اجرا کنیم. ما میتوانیم تعداد دلخواهی از دستورات elif را بر اساس نیازمان در برنامه داشته باشیم، با این حال، استفاده از دستور elif اختیاری است. ساختار دستور elif به صورت زیر است:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
مثال
# Simple Python program to understand elif statement
number = int(input("Enter the number?"))
# Here, we are taking an integer number and taking input dynamically
if number==10:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equals to 10")
elif number==50:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 50");
elif number==100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 100");
else:
print("The given number is not equal to 10, 50 or 100");
Enter the number?15
The given number is not equal to 10, 50 or 100
2 مثال
# Simple Python program to understand elif statement
marks = int(input("Enter the marks? "))
# Here, we are taking an integer marks and taking input dynamically
if marks > 85 and marks <= 100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
خروجی
Enter the marks? 89 Congrats ! you scored grade A ...