Python programming questions and answers for beginners

Python programming questions and answers for beginners. Python programming examples with solutions to understand every concept in python programming. Training is still the most important key to success when it comes to Python programming for beginners.

Python program to convert XML into CSV document

A Python program to convert XML into CSV document. Using this program we can convert xml files into csv excel file. You’ll learn how to convert data from an XML file to a CSV file using Python. Python comes with a number of essential Libraries and Packages that make it easier to utilise code in our daily life.

Python is a popular choice for a large percentage of the development community, particularly Data Scientists, due to its characteristics. Python is a popular programming language among programmers due to its dependability and simplicity. Working with enormous datasets is one of the most appealing features.

<?xml version=”1.0″?>
<programming>
<website>
<name>Inlarn</name>
<email>inlarndotcom.com</email>
</website></programming>

import xml.etree.ElementTree as ET
import pandas as pd
cols = [“name”, “email”]
rows = []
tree = ET.parse(‘programming.xml’)
root = tree.getroot()
for elem in root:
name = elem.find(“name”).text
email = elem.find(“email”).text
rows.append({“name”: name,”email”: email,})
df = pd.DataFrame(rows, columns = cols)
df.to_csv(‘inlarn.csv’)

What is XML and why it is important

In online services, XML plays a crucial role. We may effortlessly communicate data between multiple platforms with the help of this. It allows you to mark up data in a tree-like hierarchy in a systematic way. A document representation language is another name for it.

It becomes an excellent choice in online services and data interchange since it eliminates complexity and data can be accessed by multiple incompatible programmes. The XML can be a bit heavier in general. You’re sending more data, which means you’ll require greater transfer speed, extra storage space, and run time.

What is CSV and its role in office

It employs a comma to separate values, CSV stands for “comma-separated-values.” This is a commonly used file format for storing tabular data. The CSV file format is supported by all of the most common computer languages and applications.

We may need to convert the XML data to a more user-friendly format, such as CSV, because it is not readable by most customers. We can quickly import/export to a CSV file and convert any file format to a CSV file in web development. These are the steps involved in converting an XML file to a CSV file.

Python program to print the factorial number using recursion

A Python program to print the factorial number using recursion. This program can print the factorial number using the recursion method. The product of all positive integers less than or equal to a particular positive integer, indicated by that integer and an exclamation point in mathematics. As a result, factorial seven is expressed as 7!, which means 1 2 3 4 5 6 7. The factorial zero is equal to one.

Factorial is the mathematical definition of multiplying any natural number by all the natural numbers that are smaller than it, giving us n! = n * (n – 1) * (n – 2) * (n – 3)…. Finally, factorial is used to ask how many different ways you may arrange or organize a specific amount of items.

def recursion(n): 
if n == 1:
return n
else:
return n*recursion(n-1)
num = int(input("Enter a number"))
if num < 0:
print("factorial numbers can't be negative number")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recursion(num))

Output:

Enter a number 5
The factorial of 5 is 120

What is conditional statement and what is if else condition in python

Conditions are examined by the software in decision-making statements. A group of statements is executed if the condition is true, while another set of statements is run if the condition is false. Otherwise, the statements in else’s body are executed.

The if-else condition is used to execute both the true and false parts of a condition. The if block code is run if the condition is true, and the else block code is executed if the condition is false.

What is indentation in python

Indentation is used in Python to highlight code blocks. In Python, indentation is done with whitespace. All statements with the same right-hand distance belong to the same code block. If a block has to be nested further deeply, it is simply indented to the right.

Python program to remove the duplicate element in the list

Python program to remove the duplicate element in the list. Using this program we can delete or remove the duplicate element in the python list. You can use the list function in Python to generate a collection that you can manipulate for your study.

A list object is a type of data collection. While every method in Python is a function, not every function is a method. sort(): Sorts the list from top to bottom in ascending order.

things = [‘pen’, ‘eraser’, ‘pen’, ‘sharpner’, ‘whitener’]
things.remove(‘pen’)
print(‘Updated things list’, things)

Output:

Updated things list [‘eraser’, ‘pen’, ‘sharpner’, ‘whitener’]

What is list in python programming

Lists allow you to store multiple elements in a single variable. Lists are one of four built-in Python data structures for storing data collections; the other three are Tuple, Set, and Dictionary, each with its own set of features and applications.

What are the uses of python list

Lists are useful for working with a large number of related values. They enable you to keep data together, simplify your code, and apply the same procedures and actions to multiple variables at once.

How to delete the duplicate elements in list

Duplicates in Python can be removed with the dict. fromkeys() function, which creates a dictionary that removes any duplicate values. A list can also be converted to a set. To see a list with duplicates removed, you must convert the dictionary or set it back to a list.

Python program to create a stopwatch using a time function

Python program to create a stopwatch using a time function. a watch that can be started and stopped by pressing a button, allowing you to time something precisely. A stopwatch is a timepiece that can be worn on one’s wrist and is used to measure the time between activation and deactivation. A stop clock is a large digital version of a stopwatch designed for viewing from a distance, such as in a sports stadium.

import time
def time_convert(sec):
mins = sec //60
sec = sec % 60
hours = mins // 60
mins = mins % 60
print("Time Lapsed = {0}:{1}:{2}".format(int(hours),int(mins),sec))
input("Press Enter to start stopwatch")
start_time = time.time()
input("Press Enter to stop stopwatch")
end_time = time.time()
Time_taken = end_time - start_time
time_convert(Time_taken)

Output:

Press Enter to start the stopwatch
Press Enter to stop the stopwatch
Time Lapsed = 0:0:2.5889599323272705

What is import in python programming?

The import statement is the most common method of triggering the import machinery, but it is by no means the only one. When the import is used, the __import__() function searches for the module in the local scope first. The function’s return value is then reflected in the initial code’s output. The import keyword is followed by the module’s name in an import statement. This will be declared at the top of the Python file, before any shebang lines or general comments.

Time function in python

The time module’s time.time() function will be used. The documentation can be found here. The time. time() function keeps track of how many seconds have passed since the day began. We’ll need to import the time module into our code before we can use this function.

Procedures to execute the time funtion in python

We’ll bring in the time module.
To start the stopwatch, the user will press Enter.
At this point, time. time is used to set start time ().
At this point, time. time is used to set start time ().
As a result, start time now contains the number of seconds.
The clock will now be running in the background.
The user will now press Enter once more to stop the stopwatch. 
end time is set using time. time at this point (). 
end time is set using time. time at this point (). 
So, when the clock is stopped, the end time has the number of seconds that have passed.

How to remove or delete the element in python program using the list

How to remove or delete the element in python program using the list. All of the elements in a list are enclosed in square brackets [] and separated by commas. It can include an unlimited number of elements of various categories. Another list can be included as an item in a list. This is referred to as a nested list.

fruits = [‘mango’,’banana’,’watermelon’]
fruits.remove(‘mango’)
print(‘Updated fruits ‘, fruits)

Output:

Update fruits [banana,watermelon]

What is list in python programming

Multiple items can be stored in a single variable using lists. Lists are one of four built-in Python data structures for storing collections of data; the other three are Tuple, Set, and Dictionary, all of which have different properties and applications.

What is the uses of list in python programming language

When you need to work with a large number of connected values, lists are ideal. They allow you to keep data that belongs together, simplify your code, and apply the same methods and actions to several variables at the same time.

How to declare list elements in python

A list is generated in Python programming by putting all the entries inside square brackets [] and separating them with commas. It can contain any number of objects of various types, including integer, float, string, and char. Another list can be included as an item in a list.

What is remove function program in python

The remove() method in the Python programming language removes a given object from a list. It doesn’t give you anything. The method does not return a value, but it does remove the specified object from the list.

How to print a blank line in python program

How to print a blank line or empty line in python program. Using this program it will print an empty line in the output. A blank line made up of spaces and tab spaces is known as an empty line. In this article, you will learn how to print an empty line in the Python programming language. Unlike other languages, Python has several distinct techniques for printing an empty line.

print("Welcome to")
print()
print("https://noteshippo.com/")

Welcome to

https://noteshippo.com/

Python program to create a zip file with example using list

Python program to create a zip file with example using list. This will create a zip and add the element as user requested in the program. Based on the iterable objects, the zip function returns an iterator of tuples. Zip produces an empty iterator if no parameters are given. Zip provides an iterator of tuples with each tuple having only one element if only a single iterable is given. Zip provides an iterator of tuples if several iterables are given, with each tuple including elements from all of the iterables.

number_list = [1, 2, 3]
str_list = ['a','b','c']
value = zip()
value_list = list(value)
print(value_list)
value = zip(number_list, str_list)
value_set = set(value)
print(value_set)

Output:

[]
{(3, ‘c’), (2, ‘b’), (1, ‘a’)}

Write a program to print area of triangle in python

Write a program to print area of triangle in python. In Python, print the Area of Triangle program. This program will run the program using the triangle’s area and length, and it will print the triangle’s area.

print("Enter the Length")
b = float(input())
print("Enter the Height Length")
h = float(input())
a = 0.5 * b * h
print("\nArea = ", a)

Enter the Length

45

Enter the Height Length

45

Area =  1012.5

Write a program in python to print area of square

Write a program in python to print area of square. For beginners in Python programming, print the Area of Square program in Python to grasp the mathematical definition in Python.

print("Enter the requirement values")
l = float(input())
a = l*l
print("\nArea = ", a)

Enter the requirement values

45

Area=2025

Python program to print area of rectangle

Python program to print area of rectangle. Print the area of rectangle program in python programming language to understand how the program functions for mathematical operations in python programming language.

print("Enter Length of Rectangle ")
l = float(input())
print("Enter Breadth of Rectangle")
b = float(input())
a = l*b
print("\nArea = ", a)

Enter length of rectangle

45

Enter breadth of rectangle

45

Area=2025

Python program to calculate total marks percentage and grade of a student

Python program to calculate total marks percentage and grade of a student. Python software to calculate averages and percentages. The average and percentage of a student’s exam mark will be printed by this program. a per-hundred rate, number, or sum a number that expresses the central or typical value in a collection of data, such as the mode, median, or mean, and is determined by dividing the total of the values in the set by the number of values in the set.

print("Enter marks you have scored in exam ")
mark1 = int(input())
mark2 = int(input())
mark3 = int(input())
mark4 = int(input())
mark5 = int(input())
mark6 = int(input())
sum = mark1+mark2+mark3+mark4+mark5+mark6
average = sum/6
percentage = (sum/600)*100
print(end="Average Mark is ")
print(average)
print(end="Percentage Mark is ")
print(percentage)

Enter marks you have scored in exam
6
78
34
76
34 
56 
Average Mark is 47.34

percentage Mark is 47.34

Python program to print lcm of two numbers

Python program to print lcm of two numbers. The LCM numbers will be printed using the Python programming language. The simplest option for beginners is to use the Python language to solve multiple problems. The “smallest non-zero common number” that is a multiple of both numbers is the “least common multiple of two numbers”.

def compute_lcm(x, y):
if x > y:
bigger = x
else:
bigger = y
while(True):
if((bigger % x == 0) and (bigger % y == 0)):
lcm = bigger
break
bigger += 1
return lcm
a = 30
b = 20
print("Result is", compute_lcm(a, b))

Output

Result is 60

Python program to convert Celsius to Fahrenheit temperature

Python program to convert Celsius to Fahrenheit temperature. Python software for converting Celsius to Fahrenheit. In python, the software converts Celsius to Fahrenheit. in the python language, a program to search and convert celsius to fahrenheit. 

C = 29
F = (C * 1.8) + 32
print(F)

Output

84.2

Python program to convert kilometers to miles and display the output

Python program to convert kilometers to miles and display the output. The Python programming language can be used to convert kilometres to miles. In python programming, the software can convert kilometres to miles. In a Python program, this will convert the distance from kilometres to miles.

km=float(input("Enter the KMs "))
conv_fac = 0.621371
mile=km*conv_fac
print(mile)

Output

Enter the KMs 56
34.796776

Python program to generate random number between 0 and 100

Python program to generate random number between 0 and 100. In the Python programming language, generate random numbers. In python programs, the program can print random numbers. For beginners, how to create random numbers in a python program using random in the python language

import random
print(random.randint(0,100))

Output

45



Remove special characters from string python using for loop

Remove special characters from string python using for loop. In the Python programming language, remove symbols from the string. In Python, this program removes punctuation from a string.In Python, it may delete unnecessary symbols from a line or paragraph to make the sentence more understandable.

entered = "%Welcome%^#%[email protected]#$%^Inlarn"
print ("String" + entered)
symbols = "[email protected]#$;:!*%)(&^~"
for char in symbols:
entered = entered.replace(char,"")
print("Output" + entered)

Output

Welcome to Inlarn

Sum of n natural numbers using while loop in python

Sum of n natural numbers using while loop in python. In Python programming language, the sum of natural numbers. The natural numbers will be printed by this Python application. This software is essentially an extension program, but it adds in advanced features

num = 18
if num < 0:
print("Sum of the natural number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)

Output

The sum is 171

Python program to find factorial of a number using if-else

Python program to find factorial of a number using if-else. Create a factorials number program using the Python programming language. This program will help you easily locate factorial numbers in Python. It is a key concept in any programming language for beginners. The factorial of a natural number n is the product of positive integers less than or equal to n

num = 5
fact = 1
if num < 0:
print("fact is not for negative")
elif num == 0:
print("The fact of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact*i
print("The fact of",num,"is",fact)

Output

The fact of 5 is 120

Write a program to print the factorial of a given number in python

Write a program to print the factorial of a given number in python. Create a factorials number program using the Python programming language. This program will help you easily locate factorial numbers in Python. It is a key concept in any programming language for beginners. The factorial of a natural number n is the product of positive integers less than or equal to n.

Python program to print the ASCII value of a character

Python program to print the ASCII value of a character. In the Python programming language, an ASCII value is used. In the Python programming language, this software will assist you in finding ASCII values. In the computer department, ASCII values are the most significant. In computers, telecommunications equipment, and other devices, ASCII codes represent text. While they accept a large number of additional characters, most modern character encoding schemes are based on ASCII.

a = 'D'
print("ASCII value '" + a + "' is", ord(a))

Output:

ASCII value ‘D’ is 68

Python program to print the calendar of a given month and year

Python program to print the calendar of a given month and year. Calendar program written in the Python programming language. The month and date will be printed in the production of this program. The number of years we can print in a python program is unlimited; we can print every year.

import calendar
yy = 2000 
mm = 7 
print(calendar.month(yy, mm))

Output:

July 2000 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Armstrong number program in python using while loop

Armstrong number program in python using while loop. Armstrong may or may not use Python as a programming language. This program is used to determine whether or not a number is an Armstrong number. There are some basic programs that have been defined, and this program is one of them. An Armstrong number is any number with n digits that is equal to the sum of the number’s nth power of digits.

a = int(input("Enter the number "))
sum = 0
temp = a
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if a == sum:
print(a,"is an Armstrong ")
else:
print(a,"is not an Armstrong")

Output:

Enter the number 45                                                                                                                                                

45 is not an Armstrong 

Leap year or not program in python using if else condition

Leap year or not program in python using if else condition. Program in Python whether it’s a leap year or not. This software determines whether or not the year is a leap year. It will print the year in python for beginners, which is executed by the value. a year of 366 days and an intercalary day on February 29th that occurs every four years.

year = 1998
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is leap year".format(year))
else:
print("{0} is not leap year".format(year))
else:
print("{0} is leap year".format(year))
else:
print("{0} is not leap year".format(year))

Output

1998 is not leap year

Write a program to check whether a string is palindrome or not in python

Write a program to check whether a string is palindrome or not in python. In the Python programming language, write a palindrome or not software. In the Python programming language, this is the simple set program. This software is designed to disseminate information about the fundamental concepts of the Python programming language.

def isPalindrome(a):
return a == a[::-1]
a = "madam"
ans = isPalindrome(a)
if ans:
print("Yes")
else:
print("No")

Output

Yes

Write a python program to print Fibonacci series using while loop

Write a python program to print Fibonacci series using while loop. Python code to calculate the Fibonacci sequence. For beginners, this program will print the fibonacci series number in the Python programming language. The Fibonacci sequence program is the most important program for anyone learning a new programming language.

a = int(input("How many numbers do you want? ")) 
n1 = 0 
n2 = 1 
num = 2 
if a <= 0: 
print("enter the number") 
elif a == 1: 
print("Fibonacci series") 
print(n1) 
else: 
print("Fibonacci series") 
print(n1,",",n2,end=', ') 
while num < a: 
nth = n1 + n2 
print(nth,end=' , ') 
n1 = n2 
n2 = nth 
num += 1

Output:

How many numbers do you want?

5

Fibonacci sequence:
0 , 1, 1 , 2 , 3

Python program to print the prime numbers or not

Python program to print the prime numbers or not. In the Python programming language, prime or not prime. To become a python programming expert, learn basic Python program. To determine whether a number is prime or not, use the for loop in Python.A ” prime number ” is a counting number that can only be divided by itself and one.

a = 17
if a > 1:
for i in range(2,a):
if (a % i) == 0:
print(a,"is not prime number")
break
else:
print(a,"is a prime number")

Output

17 is a prime number

Python program to find largest of three numbers using if

Python program to find largest of three numbers using if. Find the biggest number in a python programming language. This is basic python programming problem to solve an advanced programs in python. It can detect the which the biggest number by comparing the numbers in python

def max(a,b): 
if a >= b: 
return a 
else: 
return b 
a = 100 
b = 200
print(max(a,b)) 

Output

200

Write a program to print the character is vowel or consonant in python

Write a program to print the character is vowel or consonant in python. Vowel or Consonant program in a python programming language. this program will check and display whether the letter is vowel or consonant. To identify the letter is actually but to do the same thing in programming language is getting complex for beginners

def VOC(x):
if (x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u'):
print("Vowel")
else:
print("Consonant")
VOC('e')
VOC('d')

Output:

Vowel                                                                                                                         

Consonant  

Write a program to swap two numbers in python

Write a program to swap two numbers in python. In the Python programming language, there is a program that swaps two numbers. In a Python program, this program can swap values. It is possible to change the value without using the temp variable or the third variable in the Python programming language.

x = 100
y = 200
x, y = y, x
print ("after swapping")
print("Value of x = ", x, " and y =", y)

Output:

After swapping                                                                                                                

Value of x =  200  and y = 100  

Python program to check whether a number is even or odd

Python program to check whether a number is even or odd. In Python, this program can determine whether a value is odd or even. Small children should figure out which number is odd and which is even, but they must learn how to do so in a programming language.

a = int(input("Enter the number to find odd or even "))
if (a % 2) == 0:
print("{0} is Even".format(a))
else:
print("{0} is Odd".format(a))

Output:

Enter a number: 45                                                                                                            

45 

Python program to add two numbers using input

Python program to add two numbers using input. In the Python programming language, two numbers are added. Learn basic python program from the beginning to become a master of python program. For beginners, we have printed the element using the third variable in Python.

a = 10
b = 10
c = a + b 
print(c) 

Output

20

Python program to print hello world

Python program to print hello world. In the Python programming language, write a hello world program. Any programming language begins with this fundamental move. A computer program that produces or shows the message “Hello, World!” is known as a “Hello, World!” program

print ('Hello World')

Output:

Hello World