Python Program | Basics of python with Program

 

Best way to learn Python-Code Gladiator's



                                                                                         Learn Python                 Coding  

Part-1

#    First We will Learn Basic Programs of python

First you have to know that which programming language you are  going to learn. Python, python language you are going to learn and it founded by Guido Van Rossum in 1991.

Python is a programming language that is easy to learn and it is open source language. 
It is work on interpreter, as it is slow as compare to compiler. 
So To learn python coding
You must have to know that in python only you have understand it's grammer, as in other programming language like C+, C++, Java there is also only have to learn grammer, but as compare to other programming language python language is simple to learn and it's grammer is also easy to access and learn. 

Now , In python there is token which is the smallest unit in python. 
Tokens is divided into 5 parts :
1) Keywords
2) Identifier
3) Literally
4) Operators
5) Punctuators



First we will understand that
What is Keywords? 
Keywords are the predefined words (it means it is already defined and you just have to use ) 
You not have to define or to make it. 
Keywords are used in python 

Note:Keywords always have to use in small letters. These are always in small alphabets. 

Such as 
pass, continue, break, else, if, eligible etc. 

2) What are identifiers
Identifiers are the building blocks, that you define the name, so let understand by example

#if you want to assign the number 3 to any variable
Example:
a=3
It means here a is a identifier, you will identify the value 3 is assign to variable a. 


3) What are Operators? 
Operators means through this something will operate. So here some examples like 
+, -, *, /, %, //, these are called arithmetic operators. 
<, > these are relational operators. 

4) What are literally? 
Before understand literals you must you must have understand that literals are two types string literal and numeric literals. 
String literals means that you write in single, double, triple quotes. 
Example:
"My name is Python"
Numeric literals are that contain only numeric data type like integer, float and evaluation. 
integers means only positive number such as 1,4,46,678,6 etc. 
float means that consist integer as well as decimal part such as 4.0,6.7,7.7 , etc. 
evaluation means it consists positive as well as negative number and also float number. 





Before writing any program if you we will use # this sign so whatever you have written in that line it's an only comment to analyse the program. It is not a part of your program. 



So Our programming Start by Basic
The basic programs to learn and to make you must have to follow algorithm. 
Using algorithms  use must have to analyse the program and break in small parts. 
So to understand algorithm
Let us take a program to use using algorithm:
We will greatest number between two numbers

Step1:Start
Step2:Declare number1 and number2
Step3:if number1>number2:
                           number1 is greatest number
           else:
                          number2 is greatest number
Step4:Stop

#so this program we will do in python language

number1=int(input("Enter a first number:"))
number2=int(input("Enter a second number:"))
if number1>number2:
 print("Number1 is greastest:",number)
else:
 print("Number2 is greatest :",number2 )

 Similarly, we will do more program

O.Write a program to print sum of the two numbers.

""" WAP TO PRINT SUM OF THE NUMBERS
"""
Testcases1:
Input
10
12
Ouput:

22

num1=2
num2=5
sum=num1+num2
print(sum)

OUTPUT:
>>>7

# Similarily you can do substraction ,multiplication and division of the numbers

#Now we will move forward to do more programs to increase your level



Q. Write a Program  to check given number is even or odd?

Testcases1:
Input

10
Ouput:
number is even 
100

Testcases2:
Input

5

Output:

Odd number
125

""" WAP to check a given number even or odd. 
if it even then find its square otherwise its cube 
num=7
it is odd and its cube 343

num=3
num/2=1.5 but modulus operator give only number after decimal
num%2=5
so it odd

"
#first we will take input number from the user and assign variable num and use input() function




num = int(input("Enter any integer positive number:"))

#Now we will test the condition using models operator if number is divisible by 2 then it will print given number is even. 

if num %2 == 0:  # test condition
    print("It is odd number and its cube is :", num**3)

# now if the condition is false then it will go to else part where it will print that given number is odd. 
else:
    print("It is a Even number and its square is:", num**2

Our Third Program


Q.WAP to perform Arithmetic operations


"""WAP to perform Arithmetic operations

"""

#input
num1 = int(input("Enter a first number:"))
num2 = int(input("Enter a second number:"))

#porcess
sum = num1 + num2

#result printing
print("Result :", sum)

# All these programms you have to run in your python app or spyder in pc



OUR Fourth Program

Q..WAP to Check a given year is Leap year or not

Testcases1:
Input
year=2000

Ouput:
 Leap year

Testcases2;
Input:
year=1999

Ouput;
Not leap year

"""
WAP to Check a given year is Leap year or not

year=1998
1998/4=494.2
but modulus operator give just decimal number
therefore
1998%4=2 it is not equal to 0
hence given year is not leap year

"""
#Input year using input() function

year = int(input("Enter a year (in four digits):"))

#Teasting the condition using if -else  structure
#If year is completely divisible by 4 then true else if condition is false then it is not leap year
if year %4 == 0 :
    print("Given year is a Leap year.")
else:
    print("It is not a Leap Year.")








Q,Write a program to swap to numbers without using third variable.

#we have to swap the numbers it means that we have to assign the value of first variable to second and value of second variable to first variable. Let us understand through example:


Testcases1:
Input
a=2
b=4

Output:
a=4
b=2
""" WAP to swap to numbers without using third variable.
a=25
b=10

After swapping
a=10
b=25
"""
#To sawp values of two element first element and second element
a=eval(input("Enter a first number:"))
b=eval(input("Enter a first number:"))
#Now use print() function to print values of first and second varable
print("Before swapping the values a=",a ,"and b = ",b)

'''
You can also follow these line by removing triple quotes
using third variable
temp =a
a=b
b=temp
'''
#without using third variable

a= a+b                 # a=25
b= a-b               # b= 25-15 =10
a= a-b              # a= 25-10 =15

print("After swapping the values a=",a ,"and b = ",b)


OUR Fifth Program

Q.

"""WAP to print your name 10 times.
ABCD of Looping Contruct


A  intialisation of control variable   i =1
B  test condition   i<=10
C  body statement    printing name 
D  increment or decrement of control variable  i = i +1

"""
i=1
while(i<=10):
    print("NAME")
    i = i +1

Q. Write a program to print Fibonacci series


Testcases1:

Input:
n=5
f=1
s=2

Output:
1 2 3 5 8

Testcases2:
Input:
n=8
f=1
s=1

Output:
1
2
3
5
8
13
21

#Let understand the program and know what is fibonacci series and how to write
#So if you know theen you also now there is must be no. of terms required
n=eval(input("Enter a number of term:"))
#F is variable or identifier to take input first term of fibonacci series
f=eval(input("Enter a first term:"))
#s is second term take input using input() function
s=eval(input("Enter a second term:"))
# Now print first and second term using print() function
print(f,"",s,end=' ')

#Take a loop to go upto no. of element 
for i in range(0,n-2):
#th is nextterm is is the sum of preceding two terms
    th=f+s                     #th is third number
    print("Fibonacci series is:",th)
    f=s
    s=th








No comments:

Powered by Blogger.