Program to get a list of prime numbers from 1 to 100

In this python function program, we will create a function to get a list of prime numbers from 1 to 100.

Prime Number: A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function prime.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. starting and ending numbers.
  4. Create a variable and assign its value equal to 0.
    If a number is divisible by 1 and by itself then it is a prime number.
  5. Use for loop with the range function to check whether a number is a prime number or not.
  6. Add only those numbers to the variable.
  7. Print the output.
  8. Pass the starting and ending numbers to the function while calling the function.
				
					def prime(lower,upper):
    print("Prime numbers between", lower, "and", upper, "are:")

    for num in range(lower, upper + 1):
        count = 0
        for i in range(1,num+1):
            if num%i == 0:
                count += 1
        if count == 2:
            print(i,end=" ")
prime(1,100)
				
			

Output :

				
					Prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
				
			

Related Articles

create dictionary output from the given string.

get a list of odd numbers from 1 to 100.

Program to create dictionary output from the given string

In this python function program, we will create a function to create dictionary output from the given string.

Dictionary:
A dictionary consists of a collection of key-value pairs.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function fun.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. string to the function.
  4. Create an empty dictionary.
  5. Create a list of the words in the string using split(” “).
  6. Use a for loop to iterate over the words in the list.
  7. Add the first letter and the last letter of the word as the key and the word as the value to the dictionary.
  8. Return the list.
  9. Pass the dictionary to the function while calling the function.
				
					def fun(string):
    a= {}
    l = string.split(" ")
    for word in l:
        a[word[0]+word[-1]] = word
    return a
fun("Python is easy li Learn")
				
			

Output :

				
					{'Pn': 'Python', 'is': 'is', 'ey': 'easy', 'li': 'li', 'Ln': 'Learn'}
				
			

Related Articles

get the square of all values in the given dictionary.

print a list of prime numbers from 1 to 100.

Python function program to get the square of all values in the given dictionary

In this python function program, we will create a function to get the square of all values in the given dictionary.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function square.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. dictionary to the function.
  4. Create an empty dictionary.
  5. Use a for loop to iterate over the keys and values of the dictionary.
  6. Add the key the empty dictionary and sqaure of the original value as the new value.
  7. Return the new dictionary.
  8. Pass the dictionary to the function while calling the function.
				
					def square(d):
    a = {}
    for key,value in d.items():
        a[key] = value**2
    return a
square({'a':4,'b':3,'c':12,'d':6})
				
			

Output :

				
					{'a': 16, 'b': 9, 'c': 144, 'd': 36}
				
			

Related Articles

get the duplicate characters from the string.

create dictionary output from the given string.

Program to get the duplicate characters from the string

In this python function program, we will create a function to get the duplicate characters from the string.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function dupli.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. string to the function.
  4. Create an empty list.
  5. Use a for loop to iterate over characters from the string.
  6. Check whether the character appears in the string more than once using count().
  7. If yes then add it to the empty list.
  8. Create a set of that list using set().
  9. Print the output.
  10. Pass the string to the function while calling the function.
				
					def dupli(string):
    list1 = []
    for char in string:
        if string.count(char) > 1:
            list1.append(char)
    print(set(list1))
dupli('Programming')
				
			

Output :

				
					{'g', 'm', 'r'}
				
			

Related Articles

get unique values from the given list.

get the square of all values in the given dictionary.

Python function program to get unique values from the given list

In this python function program, we will create a function to get unique values from the given list.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function unique.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. list to the function.
  4. Convert the list to a set to get the unique value using set().
  5. Convert the set into a list using list()
  6. Print the output.
  7. Pass the list to the function while calling the function.
				
					def unique(l):
    distinct = set(l)
    print(list(distinct))
unique([4, 6, 1, 7, 6, 1, 5])
				
			

Output :

				
					[1, 4, 5, 6, 7]
				
			

Related Articles

check whether a combination of two numbers has a sum of 10 from the given list.

get the duplicate characters from the string.

Check whether a combination of two numbers has a sum of 10 from the given list

In this python function program, we will create a function to check whether a combination of two numbers has a sum of 10 from the given list.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function check_sum.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. list and the sum number to the function.
  4. Use a for loop with the range function to iterate over the elements in the list.
  5. Use a nested for loop with the range function to iterate over the next numbers from the list i.e first loop un the first iteration will use the first element and with the help of the nested loop we will iterate over the number the numbers from the second element.
  6. Now check whether the sum of 1st element and any others elements from the nested loop is equal to 10.
  7. If yes then return True else return False.
  8. Pass the list and the sum number to the function while calling the function.
				
					def check_sum(nums, k):   
    for i in range(len(nums)):
        for j in range(i+1, len(nums)):
            if nums[i] + nums[j] == k:
                return True
    return False
print(check_sum([2, 5, 6, 4, 7, 3, 8, 9, 1], 10))
				
			

Output :

				
					True
				
			

Related Articles

get the Fibonacci series up to the given number.

get unique values from the given list.

Program to get the Fibonacci series up to the given number

In this python function program, we will create a function to get the Fibonacci series up to the given number.

What is the Fibonacci series?
The Fibonacci sequence is a set of integers (the Fibonacci numbers) that starts with a zero, followed by a one, then by another one, and then by a series of steadily increasing numbers.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function fibo.
  2. Use the def keyword to define the function.
  3. Create two variables num1, and num2, and assign their values equal to 0 and 1.
  4. Create a count variable and assign its values equal to 0.
  5. While the count variable is less than 10 print num1.
  6. Add num1 and num2 and store the result in a new variable n2.
  7. Change num1 to num2 and num2 to n2 and add 1 to the count variable.
  8. Call the function to get the output.
				
					def fibo():
    num1 = 0
    num2 = 1
    count = 0

    while count < 10:
        print(num1,end=" ")
        n2 = num1 + num2
        num1 = num2
        num2 = n2
        count += 1
fibo()
				
			

Output :

				
					0 1 1 2 3 5 8 13 21 34 
				
			

Related Articles

get the factorial of a given number.

check whether a combination of two numbers has a sum of 10 from the given list.

Program to get the factorial of a given number

In this python function program, we will create a function to get the factorial of a given number. A factorial is a mathematical operation that you write like this: n! . It represents the multiplication of all numbers between 1 and n.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function fact.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a variable named fact and assign its value equal to 1.
  5. While the input number is greater than 0 multiply the fact by the number.
  6. After each multiplication subtract 1 from the input number.
  7. Print the output.
  8. Pass the number to the function while calling the function.
				
					def fact(n):
    fact = 1
    while n > 0:
        fact *= n
        n -= 1
    print(f"Factorial of {num}: {fact}")
num = int(input("Enter a number: "))
fact(num)
				
			

Output :

				
					Enter a number: 5
Factorial of 5: 120
				
			

Related Articles

create a function with *args as parameters.

get the Fibonacci series up to the given number.

Program to create a function with *args as parameters

In this python function program, we will create a function with *args as parameters. 

What is *args?
*args allows us to pass a variable number of non-keyword arguments to a Python function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function func
  2. Use the def keyword to define the function.
  3. Pass multiple parameters to the function using *args.
    *args is used to pass multiple objects to the function.
  4. Use a for loop to iterate numbers and print the cube of each number.
  5. Pass multiple numbers to the function while calling the function.
				
					def func(*args):
    for num in args:
        print(num**3,end=" ")
func(5,6,8,7)
				
			

Output :

				
					125 216 512 343 
				
			

Related Articles

find the HCF of two numbers.

get the factorial of a given number.

Python function program to find the HCF of two numbers

In this python function program, we will find the HCF of two numbers.

HCF:
HCF stands for Highest Common Factor. HCF of two or more numbers is the greatest factor that divides the numbers.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function hcf.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. numbers to the function.
  4. Using an if-else statement check which number is smaller.
  5. Use for loop with the range function to iterate over numbers from 1 to the smaller number.
  6. During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
  7. After the loop is finished print the output.
  8. Take two numbers as input through the user and pass them to the function while calling the function.
				
					def hcf(num1,num2):
    if num1 > num2:
        smaller = num2
    else:
        smaller = num1

    for i in range(1, smaller+1):
        if((num1 % i == 0) and (num2 % i == 0)):
            hcf = i

    print(f"H.C.F of {num1} and {num2}: {hcf}")
    
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
hcf(num1,num2)
				
			

Output :

				
					Enter number 1: 24
Enter number 2: 54
H.C.F of 24 and 54: 6
				
			

Related Articles

calculate the sum of numbers from 0 to 10.

create a function with *args as parameters.