Thursday, June 10, 2021

# 2. Python Basic Operations

 

Basic Python Programs

Hi! Buddies!

Have you learned how to print strings in python using the print() function?

I hope you learned very well.

OK!

Now let's talk about python definition in a technical way and also how to perform simple mathematical operations using Python in this article.

What is python:

 Python is an Interpreted, Object Oriented, High Level and General Purpose programming language.

Are you thinking about these technical terms don't worry here we will get a clear understanding of those terms.

What is Interpreted Language?:

 The interpreter executes the statements of code line by line that’s why python gives only one error message even though our code has multiple errors

The interpreter is a program that converts high-level language to machine code and then executing.

Object-Oriented Language?:

Object-Oriented is all about creating objects. An object is a group of interrelated variables and functions.

Variables are referred to as properties or attributes of the object and functions are referred to as the behavior of the objects. These objects provide a better and clear structure of the program.

High-Level Language?:

Programmers can easily understand which means human-friendly language.

 Applications:

We can use python in machine language, GUI, software development, web development, Gaming, Application development, Enterprise, 3D CAD, AI, Business applications, Audio/Video-based applications, 3D Graphics, etc.

We can use python everywhere that’s why Python is also called general-purpose or multipurpose language.

Python supports both procedural and object-oriented programming.

There is no security in a procedural language(static language) but in an object-oriented language(dynamic language) we have more security we can apply security by access modifiers(public, private, protected). Python is mostly used everywhere in real-time projects.

Google, Facebook, DropBox, Instagram, Youtube, Twitter, etc we use python everywhere.

C language is procedural and static, but python is both procedural and object-oriented. If we want to create static projects like calculator c is enough. But for both static dynamic projects, we need python.

History:

Python is designed by Guido Van Rossum in 1991 and developed by Python Software Foundation.

“Why he named this language as Python? “

“Is there any technical reason to name this as Python?” The answer is absolute “No”.

“Then what is the reason behind this name?”

Why because the founder is a big fan of a British comedy movie called “Monty Python’s Flying Circus ”. That’s why he named it Python.

Features:

ü Easy to learn

ü Expressive language means more understandable and readable

ü Free and open source

ü Object-Oriented

ü Integrated

ü Cross-Platform which means it can run on multiple platforms like Windows, MAC OS, Linux, etc.

Comments in Python:

v Single line Comments: It is represented by “ # “

Example:  print(‘ hi ’)    # This is a single-line comment

v Multi line Comments: It is represented by triple single quotes  ‘’’ multi-line with single quotes ‘’’ or triple double quotes “”” multi-line with double quotes “””

Why comments are important: To increase readability, easy understanding, documenting.

Basic Operations:

>>> 2 + 3

        5

>>> 9 - 5

        4

>>> 4 * 6

        24

>>> 8 / 4   

# In python when we divide a number it will give float value as output.

        2.0

>>> 5 / 2

        2.5

>>> 5 // 2   

‘’’ If we want only integer value as output we will use double slash “ // “, it is      also called as floor division or integer division.’’’

        2

>>> 8 + 9 – 10

        7

>>> 8 + 9 –

Syntax Error: Invalid Syntax

>>> 8 + 2 * 3

        14

>>> (8 + 2) * 3    # It will follow  BODMAS rule

        30

>>> 2 * 2 * 2

        8

>>> 2 ** 3    # to perform power of in python we use double asterisk  “ * * “

        8

>>> 10 % 3    # Modulas

        1

>>> ‘ Divya ‘

        ‘Divya’

>>> print(‘Divya’s laptop’)

Syntax Error: Invalid Syntax

# Here we want to print a string with an apostrophe or single quote to do that we should use a single quote within double quotes like "Divya's". Or to print double quotes inside single quotes, we use double quotes within single quotes.

>>> print(“Divya’s laptop”)

        Divya’s laptop

>>> print(‘Divya’s “Laptop”’)

Syntax Error: Invalid Syntax

>>> print(‘ Divya\’s “Laptop” ’)   

“”” To skip the special meaning of that single quote we will use ‘ \ ‘  symbol “””

        Divya’s “Laptop”

>>> ‘ Divya’  + ‘ Divya ‘    # For concatenation of string we use the “ + “ symbol

        ‘DivyaDivya

>>> 5 * ‘Divya’    # to print a string multiple times

        ‘DivyaDivyaDivyaDivyaDivya

                                         (OR)

>>> ‘ Divya‘ * 5

        ‘DivyaDivyaDivyaDivyaDivya

>>> print (‘C:\docs\naveen’)    # Here it treated \n as a new line

        C:\docs

        aveen

>>> print (r ‘C:\docs\naveen’)   

‘ ‘ ‘ To print the string as it is we use raw string ‘ r ‘ which tells the python to don’t try to convert that \n into a special character just print as it is ‘ ‘ ‘

        C:\docs\naveen

                 (OR)

>>> print (r ‘C:\docs\\naveen’)

        C:\docs\naveen

 Newlines:

\n is useful, but it can be a bit of a pain if we’re trying to format lots of multiline text.
There’s another way though! Newlines are automatically added for strings created using three quotes.
For example:

print("""Python is
very easy
to learn 
and  understand""")

Output:

Python is
very easy
to learn 
and  understand

This makes it easier to format long, multi-line texts without the need to explicitly put \n for line breaks.

 


Practice programs:

1.    Write a python program to print your name?

2.  Write a python program on a conversation between two friends about how did they spend school holidays?

3.    Write a program to print your favorite song lyrics?

 

Project 1:

In an IT company, an interview is conducted to recruit python freshers. In the first round of interviews, HR asked the student a question regarding the student’s daily activities. By using the python print function write a program on daily activities line by line.

Note: Visit the next article to know solutions for practice programs and project 1.

                               please read, follow and commment.

                                      Thank you for your support

                                              HAPPY CODING!


No comments:

Post a Comment

# 7. Type conversion in Python

                                      Type Conversion in Python Python defines type conversion functions to directly convert one data type t...