Python basics questions
=======================
1) x = 10
y = 2.5
result = x + y
print(result)
What is the result of print(result)?
The result is 12.5. Both X and Y is added with arithmetic operator and printed.
2)Write a Python program that checks if a number entered by the user is positive,
negative, or zero.
a = int(input("Enter a number: "))
if a>0:
print ("A is a positive number")
elif a==0:
print ("A is Zero")
if a<0:
print("A is Negative Number")
3) a = "123"
b = 456
c = a + b
Will this code run successfully? If not, explain why and how to fix it?
No this code will not run. The variable 'a' has a string value and the variable 'b'
has a int value, it cannot be added. Either we have to change the value of a as
interger or we can convert the value of 'a' as int and then we have to print C.
4)what are all the numeric data types?
There are two types of numeric data type, Integer and float. Integer is a whole
value and float is a decimal value.
5) Get a user input and print all the details of a person.
expected output is:
My name is leela and my age is 16.currently im studying in greens
technologies.
name = input("Enter the name")
age = input("Enter the age")
course = input("Enter the institute name")
print(f"my name is {name} and my age is {age}.currently im studying in {course}")
6)where we use python?
we use python for web developing,testing,data extraction and data manipulation.
7)x = "0"
y = bool(x)
print(y)
What will be printed and why? What is the behavior of converting the string "0" to
a boolean?
True will be printed.Only empty string will be false and value inside a string will
be true always.
8)what is variables?
Variable are used for storing values or information in it. Also we can create a
vairiable by assiging value to it.
9) what is primitive data types?
a = "False"
b = "0"
c = ""
d = "None"
result = bool(a) + bool(b) + bool(c) + bool(d)
what is the final value of result? Explain how Python converts each of the strings
to a boolean value.
If we print the variable by adding all, the output will be "3". Anything inside a
string has a value and the value is 1 for all the variale except for the variable
"c".
10)what is non- primitive data types?
Primitive data types like int,float,bool,str can hold only one values where as a
non primitive data types can hold multiple values using List,Tuples,Set,dictionary.
11)get a number from a user as 5 and store it in a variable called "a" and get a
another number from a user as 6 and store it
in a variable called b.
My expected output is :
56565
a = int(input("Enter a number for a"))
b = int(input("Enter a number for b"))
print(a,b,a,b,a)
12)store a number in a variable and the print the number
a = 66
print(a)
13)what is the value of Boolean data type?
Boolean has two values which are true and false.
14)what type of error will be throw when we try to add the string with a number?
Type error.
15)write all the data types in python with an example
Integer ---> x = 10
Float -----> x = 12.5
string ----> x = "abc"
Boolean ---> x = bool(1)
List ------> x = [a,b,c]
tuples ----> x = (a,b,c)
set -------> x = {a,b,c}
Dictionary-> x = {"name":"ram","age":22}
none ------> x = none
16)write a programme to print only the middle element of the given string i.e;
"leelavathi"
17) write a condition to check whether the number is even or odd.
a = int(input("Enter a number"))
if a%2==0:
print ("The number is even")
else:
print("The number is odd")
18)Create a Python function that takes a number as input and returns whether it is
even or odd.
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "odd"
num = int(input("Enter a number:"))
output = check_even_odd(num)
print ("The number is", (output))
19) write a function to check the number is divisible by 2
expected output----[True,False,False,True,True,False]
def check_divisible_by_2 (numbers):
return numbers % 2 ==0
num = [4,5,7,8,10,11]
out = list(map(check_divisible_by_2,num))
print(out)
20)find the factorial of 6---(function and programme)
21) what is current date and time today?
22) write a function and check the string is palindrome or not?
23)Write a Python program that checks if a given year is a leap year. (A year is a
leap year if it is divisible by 4, but not divisible by 100, unless it is also
divisible by 400.)
24)Write a Python program that determines if a user-entered number is divisible by
both 3 and 5.
25) write a single line function to multiply numbers
26)Create a Python program that checks if a number is greater than, less than, or
equal to 10.
27)Write a Python program that asks for a user’s age and tells whether they are a
minor (under 18) or an adult (18 and above).
28)Write a Python function that accepts a letter and checks if it's a vowel (a, e,
i, o, u) or a consonant.
29)Create a Python program that asks for two numbers and returns the larger one.
30)write a function to print the output as a key and value pairs?
31)write a function with minimum 6 arguments?
32)what is a function? write a basic function programme?
33) what is the difference between multiple keyword and positional arguments?
34)Write a Python program that checks if a string entered by the user is a
palindrome (the string is the same when read forwards and backwards).
35)Write a Python program that asks for the current day of the week (as a string)
and returns whether it is a weekday or weekend.
36) what is isoweekday? write a program to find the isoweek day?
37)write a program to find a radius of a circle.
38)x=("java",6,7,9,"python","10")
write a program to replace the java as python
39)dict_1={"name":["leela","leelavathy","leelavathis"],"Name":
["leela","leelavathy","leelavathis"]}
write if the programme is execute or not and why?
40)write to programme to check if the given number is prime or not
41)get a name from a user i.e;javavaj only print the highly occurred element.
42)x=[1,2,3,4,5,6]
replace the number "3" with 6 by not changing the original list
43)Get a number from a user and check the given number is prime or not(hint:-use
for loop)
44)print only the odd numbers between 1-10 using while loop
45)get a number from a user
expected output
=================
*
* *
* * *
* * * *
* * * * *
* * * * * *
46) create an infinity loop
47)a)print the numbers in reverese from (1-10) using for loop
b)print the numbers in reverse from (1-10) using while loop
48)x=["python","c","c++","Django","flutter","Dart"]
expected output
===============
python
c++
Django
flutter
Dart
49)print the factorial of 5 using while loop
50)1) Write a Python program to count the occurrences of the word "Python" in the
string text = "Python is great, and Python is fun!".
51) Write a Python program to convert the string text = "hello world" to uppercase
using a string method.
52) Write a Python program that checks if the given string text = "Hello123" is
alphanumeric using a string method.
53) Write a Python program to find the index of the first occurrence of the
substring "is" in the string text = "Python is awesome".
54) Write a Python program that removes any leading and trailing whitespaces from
the string text = " Hello, World! " using a string method.
55) Write a Python program to replace all occurrences of "java" with "python" in
the string text = "I love java programming!".
56) Write a Python program to check if the string text = "Hello World!" starts with
the word "Hello".
57) Write a Python program to check if the string text = "report.pdf" ends with the
suffix .pdf.
58) Write a Python program to split the string text = "apple,banana,cherry" into a
list of words using the string method.
59)Write a Python program to find the product of all elements in a list using the
reduce function.
60)write all the types of functions and modules in python.