Skip to content

Learn Python By Example 1

Python Solved Questions

Disclaimer : This is a blog post for people learning python. The idea is to show one way of solving a problem and getting away with the task in hand.

Questions List ( Try attempting these yourself before looking at the solution )

# 1. Printing 'hello world' to the terminal.
# 2. Accept User Input with the string displayed 'Enter your name'
# 3. Print the entered name by the user to the terminal like the following : hello '< name entered by user >'
# 4. Import the library json 
# 5. Declare a string with an initial value 'hi'
# 6. Declare an empty string in python.
# 7. Write a program to accept user input and convert to int.
# 8. Declare a list of numbers.
# 9. Declare a list of numbers from 0-9.
# 10. Accept User input and print the length of the string.

1. Printing 'hello world' to the terminal.

print("hello world")

2. Accept User Input with the string displayed 'Enter your name'

name = input("Enter your name : ")

3. Print the entered name by the user to the terminal like the following : hello '< name entered by user >'

print(f"hello {name}")

4. Import the library json

import json

5. Declare a string with an initial value 'hi'

hi_txt  = "hi"

6. Declare an empty string in python.

name = str() # using contructor

name = ""

7. Write a program to accept user input and convert to int.

num = input("Enter a number: ")
num = int(num)

OR

num = int(input("Enter a number: "))

8. Declare a list of numbers.

my_list = [1,2,3]

9. Declare a list of numbers from 0-9.

my_list = [item for item in range(10)]

10. Accept User input and print the length of the string.

name = input("Enter your name : ")
print(f"Number of characters : {len(name)}")