Python - Basics

Basics of python language:

To write a c++ program, the following details are needed.

1. Command-line:

  • It describes what's going on inside a program, so that person looking at the source code does not have a hard time figuring it out.

Types of comments:

    (i) Single line comments:

     Syntax:

                 #Statements

     Example:

                   #print function

    (ii) Multiline comments:

     Syntax:

                  ‘’’Set Statements ‘’’

2.Variables:
  • A variable is nothing but it allocates the memory space and holds the values.

Syntax fro declaring variables:

                     variable_name=value;

Example:

                   a=0 #int data type

                   s=’ ‘ #String data type                     

Rules for naming the variables:

  • The first character must be an alphabet or an underscore (_).
  • No commas or blank spaces are allowed within a variable name.
  • No special symbols, an underscore can be used in a variable name.

3. Data types in python:

  • The most widely used data types in the input function is:

4. Output function:

  • This function is used to show details on the output screen for user understanding.

Syntax:

          print(“Statements”) or

          print(‘Statements’)

5. Escape sequence: 

  • This is used to align output on the screen.


Escape sequence
Description
Example
Output
\n
Newline or next line
print(“Hai! \n I am Sabrina.”)
Hai!
 I am Sabrina.
\t
Horizontal tab(one tab space)
print(“Hai\t Hello”)
Hai                         hello
\’
Display single-quotes
print(“I don\”t know”)
I don’t know
\”
Display double-quotes
print(“/“Hey/”’)
“Hey”
\\
Print slash
print(“a//b”)
a/b

















6. Input function:

  • This function is used to get value from users.

Syntax:

            Variable_name = data_type(input(“ Sentence “))

Example:

             a=int (input(“Enter a value : “))

Example program: save example.py

  1. #Write a python program to add of two elements

    name=input("Enter your name : ")

    a=int(input("Enter a value : "))

    b=int(input("Enter b value : "))

    c=a+b

    print("Add of and b value is : ",c)

Explanation:

name=input("Enter your name : ")

 or

name=str(input("Enter your name : ")) both are same for getting string values.

Output:

Example 2:

  • If need multiple inputs at one line, then refer to the below program.

Program:

  1. name=str(input("Enter calculation name : "))

    a,b=input("Enter a and b value : ").split()

    c=int(a)+int(b)

    print("Add a of and b value is : ",c)

Explanation:

  • split() function is used to separate a string from each space.
  • int(a)+int(b) means, a and b value has string so should be changed into number data type because here performing add operation.  

Output:

Example 3:

  • If displaying value at the sentence like “Add of a and b value is : 10” as “Add of 5 and 5 value is :10”

Program:

  1. name=str(input("Enter calculation name : "))

    a,b=input("Enter a and b value : ").split()

    c=int(a)+int(b)

    print(name+" of "+a+" and "+b+" value is : ",c)


Explanation:

  • In print function, the + operator is used to join variable of value to a string value.

Output:

Share:

Introduction of python programming language:

 Python programming language

What is python?

  • Python is a powerful high level, object oriented, interpreted language.
  • Proposed by Guido van Rossum in 1989.
  • It has simple, easy to use syntax and making it the perfect language for someone trying to learn computer programming for the first time.

Wherefrom the python has derived?

  • In the 1980s, Guido van Rossum was working on the Amoeba distributed OS (ADOS).
  • He has used ABC language (i.e., interpreted language). This is simple easy to understand syntax in ADOS.
  • So, he decided to create a language that was extensible (fast).
  • This is lead to the design new language which was later named “Python”.

Why the name python came?

  • Guido van Rossum was a fan of a comedy serial from the late seventies.
  • So, the name python was adopted from the same series “Monty Python’s Flying Circus.

Features of python language:

  • A simple language that is easier to learn.
  • Free and open source
  • Extensible
  • Large standard library to solve common task.
  • Object-oriented.  

Applications of python:

  • Web application (Flash, Pyramid, crome, etc)
  • Scientific and numerical computing (earthpy for earth science, astropy for astronomy, and so on).
  • Data analytics and machine learning.

Software companies that use python:

  • Google
  • Quora
  • Netflix
  • Facebook
  • Instagram
  • Dropbox etc

Reasons to choose python language:

  • Python interpreter act as a calculator because python has modes.

1.Shell mode (Python 3.5(32-bit):

  • Shell mode immediately returns the result of the command for you to enter in the shell.

2.Script mode (IDLE(Python 3.5 32-bit):

  • The script mode is used to write the script then run and also possible to immediately ran output.
  • It contains edit options and you can be also, save that py document.

Working with script mode:

Numbers:

String:

  • A string is a collection of characters or alphabetic and form a sentence or word called a string.
  • The strings are represented by single or double quotes (“word or sentence“ or
  • ’word or sentence‘).

Note:

Compiler:

  • It is used to merge all programs into machine code and checking errors.

Interpreter:

  • It is used to check error line by line and change it line by line machine codes.

If you need more details about the programming language, low-level language, middle-level Language, and high-level language means click the below links to learn. 
https://www.whereisstuff.com/2020/09/introductions-of-programming-language.html
Share:

Encapsulation in java

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public methods()

    {

    }

    }

Example program:
  1. class Account {

        private int account_number;

        private int account_balance;

     

        public void show_Data() {

            account_number=13456;

            account_balance=5000;

            System.out.println("Before deposit of "+"account XXXXX"+account_number+" number of balance :"+account_balance);

        }

     

        public void deposit(int a) {

            if (a < 0) {

                System.out.println("Given amount is wrong...");

            } else

                account_balance = account_balance + a;

            System.out.println("After deposit of current balance :"+account_balance);

        }

    }

     

    class Encapsulation{

        public static void main(String args[])

        {

            Account ac=new Account();

            ac.show_Data();

            ac.deposit(200);

        }

    }

Output:
Share:

Exception handling in java

Exception handling:

  • Exception in java is an indication of some unusual event.
  • Usually it indicates the error.
  • Various keywords used in handling the exception are :
  • try-A block of source code that is to be monitored for the exception.
  • catch  -The catch block handles the specific type of exception along with the     try block.
  • finally - It specifies the code that must be executed even though exception may or                may not occur.
  • throw - Used to throw specific exception from the program code.
  • throws - It specifies the exceptions that can be thrown by a particular method.

Syntax:

  1. class class_name

    {

    try

    {

    //Exception occures here

    }

    catch(Exception_type exception_variable)

    {

    //Exception handling here

    }

    }

Types of exception:

1) Built-in exceptions:

Before using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a = 0, b = 0, c = 0;

            a = 5;

            b = 0;

            c = a / b;

            System.out.println(a + " divide by zero of value is : " + c);

        }

    }

Output:

Handling above error by using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[])

        {

            int a = 0,b=0,c=0;

            try

            {

                a=5;

                b=0;

                c=a/b; 

            }

            catch(ArithmeticException e)

            {

                System.out.println(a+" divide by zero is infinity.");

            }

        }

Output:

Another Example program:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a[] = new int[3];

            try {

                for (int i = 1; i <= 3; i++) {

                    a[i] = i * i;

                    System.out.println("Arrsy index"+"\t"+"Added value");

                    System.out.println(i+"\t\t"+a[i]);

                }

            } catch (ArrayIndexOutOfBoundsException e) {

                System.out.println("Array index is out of bounds.");

            }

        }

    }

Output:

2) User-defined exception:

  • If you need to create user-defined exception, must be constructor method.

Example program:

  1. import java.lang.Exception;

    public class user_defined_exception extends Exception {

     

        public user_defined_exception(String msg) {

            super(msg);

        }

    }

    class Objects {

        public static void main(String args[]) {

            int age=15;

            try {

                if (age < 21) {

                    throw new user_defined_exception("Your age is very less than the condition.");

                }

            } catch (user_defined_exception e) {

                System.out.println("This is exception block.");

                System.out.println(e.getMessage());

     

            } finally {

                System.out.println("Finally exception block is end.");

            }

        }

    }

Output:
Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me