Sunday 20 February 2022

Python#01

Let us play with Python (Beginner)

Any Language is for communication between parties. Here we will go through python abilities.

What is Python?

Python is a general-purpose computer programming language.

Python is a dynamic, interpreted and object-oriented language.

Python has a rich set of APIs and wide range of libraries

Python is easy to learn and use and most suitable for data analysis


Where is Python used?

It is used in almost every technical field. The various areas of Python use are given below.

  • Data Science and data Analysis
  • Date Mining and text mining
  • Desktop Applications
  • Mobile Applications
  • Web Applications
  • Enterprise Applications
  • Machine Learning for prediction
  • Computer Vision or Image Processing Applications.
  • Speech Recognition.

Should know how to input and output for any language (Even for Computer).

Processing can be dealt with later. we will learn by exercises.

Ex 1: User Input Strings

Imagine  the scene:
When you meet a new person, normally say hello, What is your name?
He replies with "My name is Tom, What is your name ?"
You replied with "Nice name, my name is Jerry".
We will mimic this with the python  program:
Install PyCharm IDE
Create project
Give name as PYTHON
main.py is normally created with default code.
Delete the contents in main.py
Type in the below code and run
fname = input("hello, What is your name?")
name = input(f"My name is {fname}, What is your name ?")
print(f"Nice name, My name is {lname}")
Result:
hello, What is your name?Tom
My name is Tom, What is your name? Jerry
Nice name, My name is Jerry

Process finished with exit code 0

So input is reading input values. Here values Tom & Jerry, We handled strings. Next, we will handle numbers. Suppose we wrongly entered numbers instead of strings. How to track the type of value entered.

Smart python has a function for this.

# input numbers type conversion

fname = input(" Type Any value?")

print(type(fname))

fname = int(fname)

print(type(fname))

Result:

Type Any value?5
<class 'str'>
<class 'int'>

Ex 2: Handling numbers 

Now let us imagine another scene. Your son is asking for Multiplication Table 17.

Let us assume the table has to be printed like this below

1 x 17 = 17

2 x 17 = 34.

.

9 x 17 = 153.

.10 x 17 = 170 

Here we see how python does this with numbers ie. integer values

# Multiplication table (from 1 to 10) in Python

num = input("Which table You want, my dear son?")

#convert string to int
num = int(num)

# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

Result :

Which table You want, my dear son?17
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170

In this example please see the for loop and arithmetic operator *

Ex 3: Print simple interest I with P, N,R given

Suppose you want to put money in the bank as FD. Want to know how much interest will come. To print simple interest (I)  for the given Principal (P), Number of years (N), and rate of interest(R), let us code simply in python.

# Finding Simple Interest

p = int(input("enter Principal amount P :"))

n = int(input("For how many years N :"))

r = int(input("enter Rate of Interest % :"))

# calculate
for i in range(1, n + 1):
interest = (p * i * r) / 100
print('Interest = {3:.2f} for Principal = {0}, for year = {1} with rate = {2}'.format(p, i, r, interest))
Result : 

Enter Principal amount P :1000000
For how many years N :12
enter Rate of Interest % :12
Interest = 120000.00 for Principal = 1000000, for year = 1 with rate = 12
Interest = 240000.00 for Principal = 1000000, for year = 2 with rate = 12
Interest = 360000.00 for Principal = 1000000, for year = 3 with rate = 12
Interest = 480000.00 for Principal = 1000000, for year = 4 with rate = 12
Interest = 600000.00 for Principal = 1000000, for year = 5 with rate = 12
Interest = 720000.00 for Principal = 1000000, for year = 6 with rate = 12
Interest = 840000.00 for Principal = 1000000, for year = 7 with rate = 12
Interest = 960000.00 for Principal = 1000000, for year = 8 with rate = 12
Interest = 1080000.00 for Principal = 1000000, for year = 9 with rate = 12
Interest = 1200000.00 for Principal = 1000000, for year = 10 with rate = 12
Interest = 1320000.00 for Principal = 1000000, for year = 11 with rate = 12
Interest = 1440000.00 for Principal = 1000000, for year = 12 with rate = 12

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...