THE PYTHON CLASS
(Session 6, 23rd 2021)
Topics - Problems
Running Python programs
____________________________________________________________________________________________
Currently you're writing code in a text editor called TextMate.
Install Sublime instead.
Here's the link -
[Link]
Use Sublime instead of TextMate and you don't need to change anything else.
When you just want to practice one line codes, you can do it only on Terminal.
● Open Terminal
● Type python3 and press enter. (this opens something called a Python Interpreter in your Terminal)
● start typing code line by line.
To close this Python Interpreter and get back to using Terminal normally -
Type exit() and press enter
Or, press Ctrl+D together.
If nothing works, you can simply close the window. This isn’t the best way, but it will get the job done.
Once you’re done with one liner code, you should start typing code in a code editor like Sublime.
● Open Sublime.
● Save the file in your folder with any name and add extension .py at the end of your file name
● Type your code in the file
● Once done, save the file again.
To see the output of this code, we’ll need to go back to the Terminal.
● Open Terminal
● Goto your folder by typing these commands one by one (If you’re already inside the folder, skip this step.)
cd Desktop/py\ classes ,press enter
● Type python3 [Link] ,press enter (change filename to the name of your file)
If there’s no error in your code, you’ll see the result, else you’ll see some error message.
In case of an error message, Go back to your codefile, correct the code, save your file again and run code again on
Terminal using python3 [Link]
Solve each question in a separate file.
You may encounter many errors initially, try looking at your code again and see if you’ve missed some word, letter,
parenthesis, quotes, etc.
Q1). WAP(Write a Program) to take your name as input from the user and print it.
name = input('Enter your name :')
print('My name is ',name)
Q2). WAP to take two integers as input from the user, print their addition.
first = int(input('Enter first number : '))
second = int(input('Enter second number : '))
print(first+second)
Q3). WAP to take name, phone number, language as input from the user, print them with appropriate message.
name = input('Enter your name : ')
phone_no = input('Enter number : ')
lang = input('Enter your language : ')
#print('My name is ',name, 'My number is ',phone_no, 'My language is', lang)
#print('My name is{} My number is {} My language is {}'.format(name,phone_no,lang))
print(f'My name is {name} My number is {phone_no} My language is {lang}')
Q4). WAP to take radius as input, calculate diameter , perimeter and area. Print the results with some message.
radius = float(input('Enter radius : '))
diameter = radius*2
perimeter = 2*3.14*radius
area = 3.14*radius*radius
print('Diameter is :',diameter)
print('Perimeter is :',perimeter)
print('Area is :',area)
Q5). WAP to take the price and discount amount as input, print the final price after deducting the discount.
price = float(input('Enter price :'))
discount = float(input('Enter discount amount :'))
new_price = price - discount
print('Price after discount is : ',new_price)
Q6). WAP to take the price and discount percentage as input, print the final price after deducting the discount.
(You'll first need to calculate the discount out of the discount percentage.)
price = float(input('Enter price :'))
discount = float(input('Enter discount percentage :'))
discount_amount = (discount/100)*price
new_price = price - discount_amount
print('Price after discount is : ',new_price)
___________________________________________________________________________________
TODO -
Practice everything that we’ve done in previous classes, Solve questions of previous PDFs.
Q1). WAP(Write a Program) to take two numbers as input from the user and print their multiplication.
Q2). WAP to take a number as input from the user and print its square. (Session 4)
Q3). WAP to take a name as input from the user and print its first letter. (Session 2)
Q4). WAP to take a name as input from the user and print its first letter. (Session 2)
Q5). WAP to create a list of names and print the second name of this list. (Session 4)
Q6). WAP to create a list of names and change the first name to ‘PYTHON’. (Session 4)
Q7). WAP to create a list of names and add a new name to this list, print the updated list. (Session 4)
Q8). WAP to create a list of names and remove the last element from the list, print the updated list. (Session 4)