Basic Programming Fundamentals

17/4

## What is programming ?

Programming is a valid set of instructions.

ex. C , C++ programming , Java programming 

- Program is read from right to left 

ex. case1 :-     a=10;                      case2 :-         c=a+b;

                       b=20;                                           a=10;

                       c=a+b;                                         b=20;

    case1 is correct whereas case2 is incorrect its syntax is wrong bcoz 1st we've to initialize the variables then perform the operation.



##  Type of programming 

1. Code base programming 

- i/p  and  o/p  designed by the user 

 ex:-     printf();   scanf(); 

 used in :-    C, C++, Java , OOPS , procedural , Python


2. Form base programming ( tools based )

 used in :-   Dot.net / Net beans / Android Studio



##  Type of code 

   1. source code : ( designing code  ==>  printf() );

   2. logical code : ( if else ............) 



## Programming  Environment 

  User 

  Compiler 

  Interpreter 

  


##  Types of Compiler 

    1) on compiler :-  automatic  activate when the program is executed. ex. c, c++, java 

    2) off compiler :-  form base language  ( tools ) 


## Programming management 

    management tools 

    variable  ==>  looping   ==>   array   ==>  function  ==>  memory  ==>  i/ o ...... etc.



19/4

## Variable is a name of memory location where user can store different type of data value.

a=10          (int variable)

a=10.5       (float variable)

a=ram        (string variable )

a=z            (char variable)



## Types of variable:-

1. Variant Variable

    int a, float b, string c, char z

    (verify before storing the value) 

    used in c, c++


2. Non-variant variable

- Variable depend on data value.

    a=10

    a=20

    a=30

    a=ram

     JavaScript, Linux(shell prgm), Python


NOTE:-

-  String written under " "

-  Variable cannot use " "


Example:-

x=5

y="ram"

print(x)

print(y)

output:-  5

                ram


## Return the type of variable

a=10         integer

b=10.5      float

type() return the type of variable

ex. 

x=5

y="ram"

print(type(x))

print(type(y))

output:-  class 'int'

               class 'str'



## Variable terminology

1. Initialization x=5, y=3

2. Declaration x, y, z



IMP NOTE:-   Python supports multivalue variable

example:- 

x , y , z = "nagpur", "mumbai", "pune"

print(x)

print(y)

print(z)

output:-

nagpur

mumbai

pune 



## Multivariable Initialization (single value)

ex.  x = y = z = "pune"

print(x)

print(y)

print(z)

output:-   pune



In C:-

ex.  int a=10;

printf("the value of a %d ", a);   (here comma is concatenation = msg + variable)


In python:-

x=10

print("the value of x is" , x)



## Operator is used to perform the specific operation

print(10+5)   15

print(10>20)  false

print(10*5)   50

print(10-5)   5

print(10>20 && 10<20) (gives error)




20/4

## List is collection of data items (data type)

syntax:-   listvariable = ["item1", "item2" , etc]


ex. my = ["nagpur","pune","nashik","goa"]

print(my)

output:-  ["nagpur","pune","nashik","goa"]



## List function :- 

1. Length 

ex. my = ["nagpur","pune","nashik","goa"]

print(len(my))

output:- 4



## Type function

- Returns the list type

ex. my = ["nagpur","pune","nashik","goa"]

print(type(my))

output:- <class 'list'>



## Accessing the list item 

- It starts from 0 position

ex. my = ["nagpur","pune","nashik","goa"]

print(my[1])

output:- pune



## Check the list item

syntax in python :-   if condition:

ex. my = ["nagpur","pune","nashik","goa"]

    if "pune" in my:

         print("yes pune in list")

output:- yes pune in list


In C :-

if()

 {

 block of statements;

}




## Change the list item:-

ex. my = ["nagpur","pune","nashik","goa"]

my[1]="mumbai"

print(my) 

output:- ['nagpur', 'mumbai', 'nashik', 'goa']



## Insert the list item:- 

ex. my = ["nagpur","pune","nashik","goa"]

my.insert(2,"mumbai")

print(my)

output:- ['nagpur', 'pune', 'mumbai', 'nashik', 'goa']

(mumbai is inserted in the 2nd index position of the list)



## Append the list item:-

my = ["nagpur","pune","nashik","goa"]

my.append("mumbai")

print(my)

output:- ['nagpur', 'pune', 'nashik', 'goa', 'mumbai']

(here the list item is added in the end)



## Remove the list element:-

my = ["nagpur","pune","nashik","goa"]

my.remove("pune")

print(my)

output:- my = ["nagpur","pune","nashik","goa"]



## Remove the specific position:-

my = ["nagpur","pune","nashik","goa"]

my.pop(1)

print(my)

output:- ['nagpur', 'nashik', 'goa']



## Delete list

my = ["nagpur","pune","nashik","goa"]

del my

deletes everything



## Looping list(using for loop)

- We use loop variable in looping

ex1.

my = ["nagpur","pune","nashik","goa"]

for x in my:

      print(x)

output:- 

nagpur

pune

nashik

goa


ex2. 

my = ["nagpur","pune","nashik","goa"]

for x in my:

      print(my)

output:-

['nagpur', 'pune', 'nashik', 'goa']

['nagpur', 'pune', 'nashik', 'goa']

['nagpur', 'pune', 'nashik', 'goa']

['nagpur', 'pune', 'nashik', 'goa']



## List sorting

my = ["nagpur","pune","nashik","goa"]

my.sort()

print(my)

output:- ['goa', 'nagpur', 'nashik', 'pune']



## Copy list

my = ["nagpur","pune","nashik","goa"]

my1 = my.copy()

print(my1)

output:- ['nagpur', 'pune', 'nashik', 'goa']



## Join the list

my = ["nagpur","pune","nashik","goa"]

my1 = ["mango", "grapes","chiku"]

my3 = my+my1

print(my3)

output:- ['nagpur', 'pune', 'nashik', 'goa', 'mango', 'grapes', 'chiku']



## Reverse the list

my = ["nagpur","pune","nashik","goa"]

my.reverse()

print(my)

output:- ['goa', 'nashik', 'pune', 'nagpur']



## Program Management

## Condition Statement

## if...else

a = 10

b = 20

if a>b:

    print("a is greater")

else:

    print("b is greater")

 output:- b is greater



22/4

## AND operator in condition statement:-

rough example to perform

per > 60

print(FD)

if per>50 and per<60

 print(SD)

per>40 and per<50

print(TD)

else:

   print(Fail) 



executed example:- 

per = 45

if per>60:

    print("First Division")

    

elif per>50 and per<60:

    print("Second Division")

    

elif per>40 and per<50:

    print("Third Division")

    

else:

    print("Fail")

output:- 

Third Division



## Loop Statement

It is also called as Iteration or Flow Control.

1) while loop

2) for loop

Python does not have do.....while loop

NOTE:- loop uses counter variable

example:-

i = 1           

while i<10:

    print(i)

    i=i+2

output:-

1

3

5

7

9



## User input in python

1) String input

name = input("enter your name")

print("your name is " + name) 

NOTE :- in string input + is used to join 

output:-

enter your name John

John

your name is John



2) Integer input

a = int(input("enter any no."))

print("your no. is " , a)

NOTE :- In integer input (comma) , is used to join 

output:-

enter any no.121

your no. is  121


input( ) is used here in Python

ex.

a = int(input("enter any no."))

b = int(input("enter any no.")) 

print("addition of ", a+b)

output:-

enter any no.5

enter any no.3

addition of 8


example:- 

palindrome no. 

n = int(input("enter any no."))

temp = n

rev = 0

while n>0:

    dig = n%10

    rev = rev*10+dig

    n = n//10

    

if (temp==rev):

    print("This is a palindrome no.")

    

else:

    print("Not a palindrome no.")


output:-
enter any no.78987
This is a palindrome no.

Assignment

1) WAP of percentage using user input 

2) WAP any no. of table using user input

3) addition, multiply, sub of any 2 nos. 

4) WAP to input age from user choice and check age is greater than 18 then print

   you can vote otherwise you cannot vote.


1) WAP of percentage using user input.

per = int(input("enter your percentage "))
if per>60:
    print("First Division")

elif per>50 and per<60:
    print("Second Division")

elif per>40 and per<50:
    print("Third Division")

else:
    print("fail")

OUTPUT:-
enter your percentage 49
Third Division





2) WAP to print any no. of table using user input

i = 8

while i<=80:

    print(i)

    i=i+8

OUTPUT:-

8

16

24

32

40

48

56

64

72

80





3) WAP of addition, multiply, subtract of any 2 nos.

a = int(input("enter any no."))

b = int(input("enter any no."))

print("Addition of two no. is ", a+b)

print("Subtraction of two no. is ", a-b)

print("Multiplication of two no. is ", a*b)

OR

print("Result of two no. is ", a+b, a-b, a*b)

OUTPUT:-

enter any no.15

enter any no.4

Addition of two no. is  19

Subtraction of two no. is  11

Multiplication of two no. is  60





4) WAP to input age from user choice and check age is greater than 18 then print

   you can vote otherwise you cannot vote.

age = int(input("Enter your age "))
if age>18:
    print("You are eligible to vote")

else:
    print("You are not eligible to vote. Your age is under 18")

OUTPUT:-
Enter your age 25
You are eligible to vote




Comments

Popular posts from this blog

PyCharm Setup

JavaScript

Python Advance Concepts