Monday, 27 January 2020

Python program to find simple interest

Python program to find simple interest

Concept to learn: Python Basics


# Python program to find simple interest for the given principal amount, rate of interest and time in number of years.

# Inputs for P, R, N
P = 1000
R = 12
N = 1

# Calculating simple interest(SI)
SI = (P * R * N) / 100

# Print SI for the given values
print("Simple interest = ", SI)

Output of Program

Simple interest = 120.0





Saturday, 4 January 2020

Python program to print Sum of even numbers between 1 to 100

Python program to print sum of even numbers between 1 to 100 (2 + 4 + 6 + .... + 100). 


Concept to learn: Python while loop


# Python program to print sum of even numbers between 1 to 100 (2+4+6...+100) using while loop.

sum = 0
num = 0

while(num <= 100):
    sum = sum + num
    num = num + 2
print("Sum of even numbers between 1 to 100 is ", sum)


Output of Program

Sum of even numbers between 1 to 100 is 2550

Sum of odd numbers between 1 to 100

Python program to print sum of odd numbers between 1 to 100 (1 + 3 + 5 + .... + 99). 


Concept to learn: Python while loop


# Python program to print sum of odd numbers between 1 to 100 (1+3+5...+99) using while loop.

sum = 0
num = 1

while(num <= 100):
    sum = sum + num
    num = num + 2
print("Sum of odd numbers between 1 to 100 is ", sum)


Output of Program

Sum of odd numbers between 1 to 100 is 2500

Thursday, 2 January 2020

Python program to print sum of 1 to 100

Python program to print sum of 1 to 100. (Concept to learn: Python while loop)


# Python program to print sum of 1 to 100 (1+2+3+...+100) using while loop.

Program code:

sum = 0
num = 1

while(num <= 100):
    sum = sum + num
    num = num + 1
print("The sum is", sum)

Output of program:

The sum is 5050


Wednesday, 25 December 2019

Python program to read and print your name and address

Following python program will read and print name, address and mobile number of the user.


Concept to learn

input ( ) : Python input() function can be used to read user inputs. No need to specify variable data type as Python automatically identifies whether entered data is a string or a number.

Source code

# Python program to read and print your name and address using the input() function.
  
name = input("Enter your name: ") 
print(name)

address = input("Enter your address:")
print (address)

mobile = input("Enter your mobile number:")
print(mobile)

Program output

Enter your name: K Patel
K Patel
Enter your address: Ahmedabad, Gujarat, India
Ahmedabad, Gujarat, India
Enter your mobile number: 9898989898
9898989898



* * * * *


Sunday, 22 December 2019

Print Hello World

Python program to print hello world message.

Following python program will print Hello World message on output screen.

Source code
>>> print('Hello World!!!')

Program output
Hello World!!!

* * * * *

Tuesday, 3 December 2019

Python program to print square pattern of star

PYTHON PROGRAM TO PRINT Square Pattern of star


* * * *
* * * *
* * * *
* * * *

We can generate a square pattern using for loop and range() function. Number of lines to be printed will be taken from user.

Python program to print square pattern of star.


number_of_lines = int(input("Please Enter number of lines:"))

for i in range(number_of_lines):    

   for i in range(number_of_lines):        
      print('*', end = '  ')    
   print()

Program Output - 1

Please Enter number of lines:3

*  *  *  
*  *  *  
*  *  *

Program Output - 2

Please Enter number of lines:4
*  *  *  *  
*  *  *  *  
*  *  *  *  
*  *  *  *


Monday, 2 December 2019

Python program to print A to Z alphabets

Python program to print A to Z alphabets


We can generate a sequence of characters between A to Z using range() function and for loop.

range() function will start from 65 (ASCII of A) and will stop at 90 (ASCII of Z). chr() function will help us in printing ASCII character of a given integer number.

Python program to print A to Z alphabets.


for i in range(65, 91):
    print(chr(i), end=' ')

Output of Program

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z



Python program to print Z to A alphabets.


for i in range(90,64,-1):
    print(chr(i), end=' ')

Output of Program

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A


* * * * *

Sunday, 1 December 2019

Python program to print 1 to 10 numbers.

Python program to print 1 to 10 numbers


We can generate a sequence of numbers between 1 to 10 using range() function and for loop.

We can also define the starting, ending and step size as range(start,stop,step size) function parameters. By default step size is 1.

# Python program to print 1 to 10 numbers.

for i in range(1,11):
  print(i, end=' ')

Output of the program

1 2 3 4 5 6 7 8 9 10 

# Python program to print 10 to 1 numbers.

for i in range(10,0,-1):
    print(i, end=' ')

Output of the program

10 9 8 7 6 5 4 3 2 1 

Saturday, 30 November 2019

Python Dictionary

PYTHON DICTIONARY


Introduction to Python – Dictionary


Dictionary is a collection of related data item pairs. 

A python dictionary is a collection of unordered items which are changeable and indexed. In Python dictionaries are written using curly brackets, and they have keys and values.


Important properties of Python - Dictionary


Un-ordered – Elements/items stored in a python dictionary are not kept in any particular order.
Changeable (mutable) – Structure and elements can be changed in place; elements can be added and removed.
Accessed by keys – Dictionary items are accessed by the keys, not by their position number (index).
Nesting Properties - Dictionaries can be nested, i.e. a dictionary can contain another dictionary in it.

Create a Python Dictionary


The dictionaries are created using curly brackets. Curly brackets contains pairs of key-value. 

>>> dist1 = {"brand":"Maruti", "model":"Desire", "year":2019}

>>> print(dist1)
{'brand': 'Maruti', 'model': 'Desire', 'year': 2019}

Accessing elements of a Python Dictionary


Following syntax can be used to access the individual elements of the python dictionary.

>>> print(dist1['brand'])
'Maruti'

>>> print(dist1['model'])
Desire

>>> print(dist1['year'])
2019

Following functions can be used to display all keys and values of the python dictionary.

>>> dist1.keys()
dict_keys(['brand', 'Model', 'Year'])

>>> dist1.values()
dict_values(['Maruti', 'Desire', 2019])