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:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me