Thursday, 21 December 2023

Top 10 Python Programming question answer

 

1. What are the key features of Python?


1. Open source and free: The Python programming language is freely available on the official website.

2. Interpreted Language: Python is an Interpreted Language since its code is executed line by line.

3. High-Level Programming Language: When we build Python programmed, we don't need to understand the system architecture or manage memory.

4. Python is a fairly simple language to learn when compared to other languages such as C, C#, JavaScript, Java, and so on.

5. Extensible feature: We can write Python code in C or C++ language and build the code in C/C++ language.

6. Comprehensive Standard Library: Python includes numerous libraries such as NumPy, pandas, and matplotlib.

7. Dynamically Typed Language: We don't need to declare the variable type.

2. How do you write comments in python? And Why Comments are important?


Comment in python:
Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program.

Single line Comments:
Comments in Python begin with a hash tag (#) and continue to the end of the line.

Multiline Comments:
1) We can use hashtag (#) to write multiline comments in Python. Each and every line will be considered as a single-line comment.
   
2) we ca use the strings with triple quotes (""" """) as multiline comments.

Importance:
1) Using comments in programs makes our code more understandable.
2) It makes the program more readable which helps us remember why certain blocks of code were written.
3) comments can also be used to ignore some code while testing other blocks of code.

3. What do you mean by Python literals?


Literals are the constant values, or the variable values used in a Python code.

1) String literals: String literals are characterized either by '', or "" surrounding them. The string literals can be Single line or Multiple line strings. 

2) Numeric literals: Numeric Literals in Python can be of three numeric types; int, float, complex.

3) Boolean literals: Boolean literals can True or False.

4) Special literals: None is a special literal defined in Python to represent a NULL value.

5) collection literals: There are four different types of literal collections; List literals, Tuple literals, Dict literals, Set literals.

4. What are the Escape Characters in python?


Escape characters are used to indicate that the characters after them are encoded differently.

5. Write a Python program to reverse words in a string


string = "slicing is the first and easiest way to reverse a string in python"

string = (string[::-1])

string

6. Write a Python program to swap cases of a given string


string = "Write a Python program to swap cases of a given string"

string = string.swapcase()

string

7. Write a program to find the length of the string "machine learning" with and without using len function.



string = "machine learning"
count = 0
for i in string :
    count = count + 1
print(count)


8. Write a Python program to count the occurrences of each word in a given sentence.


from collections import Counter

c = Counter(["Write", "a", "Python", "program", "to", "count", "the", "occurrences", "of", "each", "word", "in", "a", "given", "sentence","."])

print(c)

9. Python program to Count Even and Odd numbers in a string


string = input( "enter a string :")

even = 0
odd = 0

for i in range (0, len(string)):
    if i %2 ==0:
        
        even+=1
    else:
        odd+=1
        
print("Even no. count is :", even)  
print("Odd no. count is:" ,odd)

10. How do you check if a string contains only digits?


string = "python and machine learning 12345"

print(string.isdigit())


Top 10 Pandas Question Answer

  1. Define the Pandas/Python pandas? Pandas is an open-source library for high-performance data manipulation in Python. 2. What are the dif...