Thursday, 27 February 2025

Write a program to append the text "This is an additional line." to an existing file.

 with open("output.txt", "a") as file:    file.write("This is an additional line.\n")print("Text appended successfully!")#source code --> clcoding.com  Code Explanation:Open the File in Append Mode ("a")The open("output.txt",...

Create a program that writes a list of strings to a file, each on a new line.

 lines = ["Hello, World!", "Python is great!", "File handling is useful!", "End of file."]with open("output.txt", "w") as file:    for line in lines:        file.write(line + "\n")print("Data written to output.txt...

Write a program to read the contents of a file and display them on the screen.

 with open("example.txt", "r") as file:    content = file.read()print("File Contents:")print(content)#source code --> clcoding.com  Code explanation:Opening the File:We use open("example.txt", "r") to open the file...

Write a Python program to create a text file named example.txt and write the text "Hello, World!" into it.

 with open("example.txt", "w") as file:    file.write("Hello, World!")print("File created and text written successfully!")#source code --> clcoding.com  Code Explanation:Opening the File:We use open("example.txt",...

Write a program to remove duplicates from a list using a set.

 numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]unique_numbers = list(set(numbers))print("List without duplicates:", unique_numbers)#source code --> clcoding.com  Code Explanation:Define a List with Duplicatesnumbers = [1,...

Write a program to print all keys and values of a dictionary using a loop

 student = {"name": "John", "age": 20, "grade": "A"}for key, value in student.items():    print(f"Key: {key}, Value: {value}")#source code --> clcoding.com  Code Explanation:Define the Dictionarystudent = {"name":...

Given a dictionary student = {"name": "John", "age": 20, "grade": "A"}, print the value of "age".

 student = {"name": "John", "age": 20, "grade": "A"}print(student["age"])#source code --> clcoding.com  Code Explanation:Creating the Dictionarystudent = {"name": "John", "age": 20, "grade": "A"}A dictionary named student...

Create a dictionary to store student details like name, age, and grade. Print the dictionary.

 student = {    "name": "Alice",    "age": 20,    "grade": "A"}print("Student Details:", student)#source code --> clcoding.com  Code Explanation:Step 1: Creating a Dictionarystudent = { ...

Write a Python program that takes an integer as input and checks if it is NOT an even number.

 num = int(input("Enter a number: "))if not (num % 2 == 0):      print("The number is NOT even (it is odd).")else:    print("The number is even.")#source code --> clcoding.com Code Explanation:Taking...

Write a Python program that takes a person's age as input and checks if they are NOT eligible to vote (less than 18 years old).

 age = int(input("Enter your age: "))if not (age >= 18):    print("You are NOT eligible to vote.")else:    print("You are eligible to vote.")#source code --> clcoding.com Code Explanation:Taking Input from...

Write a Python program that takes a number as input and checks if it is NOT a prime number.

 num = int(input("Enter a number: "))if num < 2 or any(num % i == 0 for i in range(2, int(num ** 0.5) + 1)):    print("The number is NOT a prime number.")else:    print("The number is a prime number.")#source code...

Write a Python program that takes a year as input and checks if it is NOT a leap year.

 year = int(input("Enter a year: "))if not ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):    print("The year is NOT a leap year.")else:    print("The year is a leap year.")#source code --> clcoding.com Code...

Write a Python program that takes two numbers as input and checks if the first number is NOT greater than or equal to the second number.

 num1 = float(input("Enter the first number: "))num2 = float(input("Enter the second number: "))if not (num1 >= num2):    print("The first number is NOT greater than or equal to the second number.")else:    print("The...

Write a Python program that takes a number as input and prints its multiplication table (up to 10) using a for loop.

 num = int(input("Enter a number: "))# Print multiplication tableprint(f"Multiplication Table of {num}:")for i in range(1, 11):    print(f"{num} x {i} = {num * i}")#source code --> clcoding.com Code Explanation:Take User...

Write a Python program that asks the user for a number n and calculates the sum of all numbers from 1 to n using a while loop.

 n = int(input("Enter a number: "))sum_numbers = 0i = 1while i <= n:    sum_numbers += i    i += 1print(f"The sum of numbers from 1 to {n} is {sum_numbers}")#source code --> clcoding.com Code Explanation:Take...

Write a Python program that takes a string as input and prints it in reverse order using a for loop.

 text = input("Enter a string: ")reversed_text = ""for char in text:    reversed_text = char + reversed_textprint(f"Reversed string: {reversed_text}")#source code --> clcoding.com Code Explanation:Take User Input:text...

.Write a Python program that takes 10 numbers as input from the user (using a for loop) and counts how many are even and how many are odd.

even_count = 0odd_count = 0for i in range(10):    num = int(input(f"Enter number {i+1}: "))        if num % 2 == 0:        even_count += 1    else:       ...

Write a function multiply_by_two() that takes a number as input and prints its double.

 def multiply_by_two(number):    """This function takes a number and prints its double."""    result = number * 2      print("Double of", number, "is", result)  multiply_by_two(5)multiply_by_two(10)#source...

Write a function calculate_area() that takes length and width as arguments and returns the area of a rectangle.

 def calculate_area(length, width):        area = length * width      return area result = calculate_area(5, 3)print("Area of the rectangle:", result)#source code --> clcoding.com Code...

Write a function find_largest() that takes three numbers as arguments and returns the largest number.

 def find_largest(a, b, c):    """This function takes three numbers as arguments and returns the largest number."""    return max(a, b, c)  result = find_largest(10, 25, 8)print("The largest number is:", result)#source...

Write a function print_even_numbers() that prints all even numbers from 1 to 20 using range().

 def print_even_numbers():    """This function prints all even numbers from 1 to 20."""    for num in range(2, 21, 2):          print(num, end=" ")  print_even_numbers()#source...

Write a function count_vowels() that takes a string and returns the number of vowels (a, e, i, o, u).

 def count_vowels(string):    """This function counts the number of vowels (a, e, i, o, u) in a given string."""    vowels = "aeiouAEIOU"      count = 0      for char in string:   ...

Write a function square_number(n) that returns the square of a number.

 def square_number(n):    return n * n print(square_number(5))  print(square_number(10)) #source code --> clcoding.com Code Explanation:Define the Function:def square_number(n): → This defines a function...

Write a function reverse_string(s) that takes a string and returns the reversed version of it.

 def reverse_string(s):    return s[::-1] print(reverse_string("hello"))  print(reverse_string("Python")) print(reverse_string("12345"))  #source code --> clcoding.com Code Explanation:Define...

Write a program that converts a given string to uppercase and lowercase using upper() and lower().

 text = input("Enter a string: ")uppercase_text = text.upper()lowercase_text = text.lower()print("Uppercase:", uppercase_text)print("Lowercase:", lowercase_text)#source code --> clcoding.com Code Explanation:Take user inputinput("Enter...

Write a program that takes a name and age as input and prints a sentence using an f-string

 name = input("Enter your name: ")age = input("Enter your age: ")print(f"My name is {name} and I am {age} years old.")      #source code --> clcoding.com Code Explanation:Take user input for name and agename...

Create a dictionary to store student details like name, age, and grade. Print the dictionary.

 details = {"name": "Alice", "age": 25, "grade": "A"}print("Dictionary Example:", details)#source code --> clcoding.com Code Explanation:Creating a Dictionary:details = {"name": "Alice", "age": 25, "grade": "A"}A dictionary named...

Given a dictionary student = {"name": "John", "age": 20, "grade": "A"}, print the value of "age".

 student = {"name": "John", "age": 20, "grade": "A"}print("Age:", student["age"])#source code --> clcoding.com Code Explanation:Define the Dictionary:student = {"name": "John", "age": 20, "grade": "A"}A dictionary named student...

Write a program to print all keys and values of a dictionary using a loop.

 student = {"name": "John", "age": 20, "grade": "A"}for key, value in student.items():    print(f"{key}: {value}")#source code --> clcoding.comCode Explanation: Define the Dictionary:student = {"name": "John", "age":...

Write a program to remove duplicates from a list using a set.

 numbers = [1, 2, 3, 4, 5, 3, 2, 1, 6, 7, 8, 5]unique_numbers = list(set(numbers))print("List after removing duplicates:", unique_numbers)#source code --> clcoding.com Code Explanation:Define the List with Duplicates:numbers =...

Write a Python program that takes two numbers as input and checks if the first number is completely divisible by the second number

 num1 = int(input("Enter the first number: "))num2 = int(input("Enter the second number: "))if num2 == 0:    print("Division by zero is not allowed.")elif num1 % num2 == 0:    print(f"{num1} is completely divisible...

Write a program that takes a string as input and prints the first and last character of the string

 text = input("Enter a string: ")if len(text) > 0:    print("First character:", text[0])      print("Last character:", text[-1])  else:    print("The string is empty!")#source code -->...

Write a function sum_of_list(lst) that returns the sum of all elements in a list

 def sum_of_list(lst):    return sum(lst)  print(sum_of_list([1, 2, 3, 4, 5]))print(sum_of_list([10, 20, 30])) print(sum_of_list([-5, 5, 10])) #source code --> clcoding.com Code Explanation:Define...

Write a function is_even(n) that returns True if a number is even, otherwise False.

 def is_even(n):    return n % 2 == 0  print(is_even(8))  print(is_even(7)) print(is_even(0))  #source code --> clcoding.com Code Explanation:Define the Function:def is_even(n): → This...

Wednesday, 26 February 2025

Write a Python program that takes two words as input and compares their lengths.

word1 = input("Enter the first word: ")word2 = input("Enter the second word: ")len1 = len(word1)len2 = len(word2)if len1 > len2:    print(f'"{word1}" is longer than "{word2}".')elif len1 < len2:    print(f'"{word2}"...

Top 5 Skills You Must Know Before You Learn Python

 IntroductionPython is one of the most beginner-friendly programming languages, but having some foundational skills before diving in can make your learning journey smoother and more effective. Whether you're a complete beginner or transitioning...

Write a Python program that takes a number as input and checks if it is between 50 and 100

 num = float(input("Enter a number: "))if 50 <= num <= 100:    print("The number is between 50 and 100.")else:    print("The number is NOT between 50 and 100.")#source code --> clcoding.com Code Explanation:Taking...

Popular Posts

Categories

100 Python Programs for Beginner (98) AI (40) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (198) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1054) Python Coding Challenge (460) Python Quiz (127) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)