EXAMPLES: Example 1: a = “Vivek” is_red = traffic_light_color == "red" b= “Joshi” is_orange = traffic_light_color == "orange" c= a + “ ” + b is_green = traffic_light_color == "green" print(c) if is_red: Example 2: print("Stop") a = 10 elif is_orange: b = 20 print("Ready") c=a+b elif is_green: print c print("Go") else: Example 3: print("Unknown option") age = 21 if age > 18: Example 8: print("This person is an adult") a = int(input("Enter the value of a: ")) else: b = int(input("Enter the value of b: ")) print("This person is a child") c=a+b print (c) Example 4: score = 49 Example 9: grade = 'B+' count = 1 print(grade) while count <= 10: if score > 80: print(count) grade = "A+" count = count + 1 if score > 60 and score <= 80: grade = "A" Example 10: print(grade) numbers = [1, 2, 3] for i in numbers: Example 5: print(i) day = "monday" if day == "sunday" or day == "saturday": Example 11: print("The day is a weekend") for i in range(1,11): else: print(i) print(“The day is a weekday”) Example 12: Example 6: fruits = ["apple", "banana", "cherry"] score = 90 for x in fruits: has_passed = score > 40 print(x) has_distinction = score > 70 if has_passed: Example 13: if has_distinction: mix_list = [1, "a", 0.1, -1] print("Has Passed with distinction!") for i in mix_list: else: print(i) print("Has Passed!”) else: Example 14: print("Has Failed") x=2 while x < 7: Example 7: print(x) traffic_light_color = "red" x = x +1 Example 15: Write a program to find a name in a given list: name = 'rohan' names = ['tony', 'rahul', 'vijay', 'raghu', 'rohan', 'yuvan'] for n in names: if n == name: print(n, " present is this list") Example 16: Calculate total number of even numbers between 1 to 10000: count = 0 for i in range(1, 10001): if i % 2 ==0: count= count + 1 print(count) Example 17: # Python program to find the largest of 3 numbers that have been input by the user num1 = int(input(“Please enter the first number: “)) num2 = int(input(“Please enter the second number: “)) num3 = int(input(“Please enter the third number: “)) # Using conditionals and conditions to find the largest number if (num1 >= num2) and (num1 >= num3): largest=num1 elif (num2 >= num1) and (num2 >= num3): largest = num2 else (num3 >= num1) and (num3 >= num2) largest = num 3 print(“The largest number is “, largest) Example 18: #Python program to find out if a number input is positive, 0 or negative using conditionals num=int(input(“Please enter a number: “)) if (num >0): print(“Number is positive”) elif (num == 0): print(“Number is 0”) else: print(“Number is negative”) Example 19: #Python program to find out if a number input is positive, 0 or negative using nested if statements num=int(input(“Please enter a number: “)) if (num>=0): if (num==0): print(“number is zero”) else: print(“number is positive) else: print(“number is negative) Example 20: # Python code to find the factorial of a number num=int(input(“Please enter a number: “)) if (num<0): print(“Sorry, factorials does not exist for negative numbers”) elif (num==0): print(“Factorial of 0 is 1) else: factorial = 1 for i in range(1, num+1): factorial = factorial *i print(factorial) Example 21: # Python code to find whether the number is a prime number: num = int(input(“Please enter a number: ”)) is_prime = True if (num < 0): print(“Negative numbers cannot be prime. Please try again.”) elif (num == 0): Print(“0 is not prime because it has more than 2 positive divisors.”) elif (num == 1): print(“1 is not prime because it has less than 2 positive divisors.”) else: for i in range(2, num): if (num % i == 0): is_prime = false break if is_prime: print(“It is a prime number.”) else: print(“It is not a prime number.”) Example 22: # Python code to find whether the year is a leap year. year = int(input(“Enter the year: ”)) if (year <= 0): print (“Years can’t be negative or 0”) else: if (year % 4 == 0): if (year % 100 == 0): if (year % 400 == 0): print(“It is a leap year”) else: print(“It is not a leap year”) else: print(“It is a leap year”) else: print(“It is not a leap year”) Example 23: # Python code to find BMI. # Get inputs from the user for their height and weight weight = round(input(“Please enter your weight in Kilograms: “) temp= round(input(Please enter your height in feet: “) # Converting height to meters height = temp * 0.3048 #Calculating BMI bmi = float(weight/height * height) print(“Your Body Mass Index is : “ “, (bmi)) # Health Indicator if (bmi < 18.5): print(“Your Body Mass Index is : “ + str(bmi) + “You are underweight.”) elif (bmi< 24.9): print(“Your Body Mass Index is : “ + str(bmi) + “Your weight is normal”) else: print(Your Body Mass Index is : “ +str(bmi)) + “You are overweight”) Example 24: # Python code to find total marks. marks_list = [25, 46, 79, 80, 56, 90] index = 0 total = 0 # Using while loop to calculate total. while (index < len(marks_list)): total = total + marks_list(index) index = index + 1 print("Total Marks: ", total) Example 25: Example 26: # Python code to swap numbers. def swap_numbers(a, b): a, b = b, a return a, b print swap_numbers(1, 2) (Also Try: (2, 1) )
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-