PROGRAMY NAJS Flashcards
Write a program to take a number as input, and output a list of all the numbers below that number, that are a multiple of both, 3 and 5.
Sample Input
42
Sample Output
[0, 15, 30]
x = int(input())
list=[i for i in range(x) if i % 15 == 0]
print(list)
program do fiszek 1
class flashcard: def \_\_init\_\_(self, word, meaning): self.word = word self.meaning = meaning def \_\_str\_\_(self):
#we will return a string return self.word+' ( '+self.meaning+' )'
flash = []
print(“welcome to flashcard application”)
#the following loop will be repeated until #user stops to add the flashcards while(True): word = input("enter the name you want to add to flashcard : ") meaning = input("enter the meaning of the word : ")
flash.append(flashcard(word, meaning)) option = int(input("enter 0 , if you want to add another flashcard : ")) if(option): break
# printing all the flashcards print("\nYour flashcards") for i in flash: print(">", i)
program do fiszek 2
import random
class flashcard: def \_\_init\_\_(self):
self.fruits={'apple':'red', 'orange':'orange', 'watermelon':'green', 'banana':'yellow'}
def quiz(self): while (True):
fruit, color = random.choice(list(self.fruits.items())) print("What is the color of {}".format(fruit)) user_answer = input() if(user_answer.lower() == color): print("Correct answer") else: print("Wrong answer") option = int(input("enter 0 , if you want to play again : ")) if (option): break
print(“welcome to fruit quiz “)
fc=flashcard()
fc.quiz()
Python Program to Calculate the Area of a Triangle
Python Program to find the area of triangle
a = 5 b = 6 c = 7
# Uncomment below to take inputs from the user # a = float(input('Enter first side: ')) # b = float(input('Enter second side: ')) # c = float(input('Enter third side: '))
# calculate the semi-perimeter s = (a + b + c) / 2
# calculate the area area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The area of the triangle is %0.2f' %area)
Python Program to Generate a Random Number
Program to generate a random number between 0 and 9
# importing the random module import random
print(random.randint(0,9))
Python Program to Check Leap Year
Python program to check if year is a leap year or not
year = 2000
# To get year (integer input) from the user # year = int(input("Enter a year: "))
# divided by 100 means century year (ending with 00) # century year divided by 400 is leap year if (year % 400 == 0) and (year % 100 == 0): print("{0} is a leap year".format(year))
# not divided by 100 means not a century year # year divided by 4 is a leap year elif (year % 4 ==0) and (year % 100 != 0): print("{0} is a leap year".format(year))
# if not divided by both 400 (century year) and 4 (not century year) # year is not leap year else: print("{0} is not a leap year".format(year))
Python Program to Check Prime Number
Liczby pierwsze
for…else
Aby sprawdzić, czy liczba jest liczbą pierwszą, należy sprawdzić czy posiada ona dzielniki inne niż 1 i samą siebie.
Należy pamiętać, że 0 i 1 nie są liczbami pierwszymi, natomiast 2 jest liczbą pierwszą.
Program to check if a number is prime or not
num = 407
# To take input from the user #num = int(input("Enter a number: "))
# prime numbers are greater than 1 if num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number")
# if input number is less than # or equal to 1, it is not prime else: print(num,"is not a prime number")
Program to check if a number is prime or not
Using a flag variable
Program to check if a number is prime or not
num = 29
# To take input from the user #num = int(input("Enter a number: "))
# define a flag variable flag = False
# prime numbers are greater than 1 if num > 1: # check for factors for i in range(2, num): if (num % i) == 0: # if factor is found, set flag to True flag = True # break out of loop break
# check if flag is True if flag: print(num, "is not a prime number") else: print(num, "is a prime number")
Python Program to Print all Prime Numbers in an Interval
Python program to display all the prime numbers within an interval
lower = 900 upper = 1000
print(“Prime numbers between”, lower, “and”, upper, “are:”)
for num in range(lower, upper + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)
Python Program to Print the Fibonacci sequence
Program to display the Fibonacci sequence up to n-th term
nterms = int(input(“How many terms? “))
# first two terms n1, n2 = 0, 1 count = 0
# check if the number of terms is valid if nterms <= 0: print("Please enter a positive integer") # if there is only one term, return n1 elif nterms == 1: print("Fibonacci sequence upto",nterms,":") print(n1) # generate fibonacci sequence else: print("Fibonacci sequence:") while count < nterms: print(n1) nth = n1 + n2 # update values n1 = n2 n2 = nth count += 1
Python Program to Make a Simple Calculator
Program make a simple calculator
# This function adds two numbers def add(x, y): return x + y
# This function subtracts two numbers def subtract(x, y): return x - y
# This function multiplies two numbers def multiply(x, y): return x * y
# This function divides two numbers def divide(x, y): return x / y
print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide")
while True: # take input from the user choice = input("Enter choice(1/2/3/4): ")
# check if choice is one of the four options if choice in ('1', '2', '3', '4'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: "))
if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2))
# check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") if next_calculation == "no": break
else: print("Invalid Input")
Python Program to Display Calendar
Program to display calendar of the given month and year
# importing calendar module import calendar
yy = 2014 # year mm = 11 # month
# To take month and year input from the user # yy = int(input("Enter year: ")) # mm = int(input("Enter month: "))
# display the calendar print(calendar.month(yy, mm))
Python Program to Sort Words in Alphabetic Order
Program to sort alphabetically the words form a string provided by the user
my_str = “Hello this Is an Example With cased letters”
# To take input from the user #my_str = input("Enter a string: ")
# breakdown the string into a list of words words = [word.lower() for word in my_str.split()]
# sort the list words.sort()
display the sorted words
print(“The sorted words are:”)
for word in words:
print(word)
Python Program to Find the Size (Resolution) of a Image
def jpeg_res(filename): """"This function prints the resolution of the jpeg image file passed into it"""
# open image for reading in binary mode with open(filename,'rb') as img_file:
# height of image (in 2 bytes) is at 164th position img_file.seek(163)
# read the 2 bytes a = img_file.read(2)
# calculate height height = (a[0] << 8) + a[1]
# next 2 bytes is width a = img_file.read(2)
# calculate width width = (a[0] << 8) + a[1]
print(“The resolution of the image is”,width,”x”,height)
jpeg_res(“img1.jpg”)
Python Program to Copy a File
from shutil import copyfile
copyfile(“/root/a.txt”, “/root/b.txt”)