Introduction to Python

Introduction to Python

Hi myself Harsh a Python programmer and today in this blog I will tell about basics of python Happy learning!!

Harsh Chaudhary's photo
Harsh Chaudhary
·Jan 31, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

What is python

Language which is easy to understand and you can also say a simple language and the pseudo code nature of this language makes it more easy to learn and understandable for beginners.

Python is also a high level language and other high-level languages you might have heard of are C, C++, Perl, and Java.

The features of this language are:-

• Simple syntax

• Programs are clear and easy to read

• Has powerful programming features

• Companies and organizations that use Python include YouTube, Google, Yahoo, and NASA.

• Python is well supported and freely available at www.python.org

• Free and open source language and have fun to work with that

  How to install python

  Go to the site of python that is python.org python.org and click on download button to install this python.

History of python:-

• Guido van Rossum – Creator

• Released in the early 1990s.

• Its name comes from a 1970s British comedy sketch television show called Monty Python’s Flying Circus

Computers can only run programs written in low-level languages, so programs written in a high-level language have to be processed before they can run.

So two kinds of program translator to convert from high-level languages into low-level languages:

– Interpreters

– Compilers

Now we see what is interpreter:-

An interpreter processes the program by reading it line by line

SOURCE CODE ------ INTERPRETER ------ OUTPUT

Now we see what is compiler:-

Compiler translates completely a high level program to low level it completely before the program starts running

SOURCE CODE ----- COMPILER ----- OBJECT CODE ----- EXECUTOR ----- OUTPUT A compiler translates source code into object code which is run by a hardware executor.

• High-level program is called source code

• Translated program is called the object code or the executable.

Why python is considered as an interpreted language

Python is considered as an interpreted language because Python programs are executed by an interpreter.

Now let's talk about the uses of Interpreter......

There are two ways to use the interpreter:

interactive mode and script mode

Interactive mode:-

In interactive mode, you type Python programs and the interpreter displays the result: . >>> 1 + 2

3

The shell prompt, >>>, is the prompt the interpreter uses to indicate that it is ready. If you type 1 + 2, the interpreter replies 3.

Script Mode:-

Store code in a file and use the interpreter to execute the contents of the file, which is called a script.

Python scripts have names that end with .py .

In Interactive mode we can type small pieces of code and execute them immediately but for anything more than a few lines, should save your code as a script so you can modify and execute it in future.

In script mode like file name is abc so we have to save them as abc.py then execute that code.

Problem:-

If a=2 and b=3 then what is a+b ? write a python code to find this .

Python code:-

a=2

b=3

c=a+b

print(c)

When you click on run button it asks for file name and we take this as add.py and execute this

Output:-

5

What is an Identifier

An identifier is a sequence of one or more characters used to name a given program element.

Some points we should remember in identifier are:-

(1) In Python, an identifier may contain letters and digits, but cannot begin with a digit.

(2) Special underscore character can also be used

Example : flower, red, rose1, red_rose

Rules for Identifier:-

Python is case sensitive, thus, Line is different from line.

The The underscore character, _, is also allowed to aid in the readability of long identifier names.

*Underscore should not be used as the first character

*Spaces are not allowed as part of an identifier.

Valid Identifiers:- redrose, redrose , redrose12, red_rose

Invalid Identifiers:- 'redrose' , red rose, 12rosered, _redrose

reason invalid :- quotes not allowed, spaces not allowed, cannot being with a digit, cannot being with underscore

Keywords:-

Identifier that has pre-defined meaning in a programming language.

keywords cannot be used as “regular” identifiers.

TOP 15 KEYWORDS IN PYTHON:-

AND - used for logical and operation

OR - used to test object identity.

NOT - Check if a condition is not met with a Python “if” statement.

BREAK - used to bring the control out of the loop when some external condition is triggered

CONTINUE - used to control the execution of the loops.

TRUE - This keyword represents the Boolean value ‘true’.

FALSE - This keyword represents the Boolean value ‘false’.

IF - represents a condition instance in python.

ELSE - represent the false execution of an if statement.

ELIF - represents the false execution with a different additional condition check for an if statement.

FOR - keyword is used to represent the for loop instance

WHILE - keyword is used to represent the while loop instance

TRY - keyword is used to kick start the process of parsing the code for an exc

IMPORT - Import keyword is used for importing the necessary libraries into the program

PRINT - print keyword is used for displaying output in the terminal

VARIABLES:- A name that is assigned to a value or variable is a name (identifier) that is associated with a value.

                                    num - 10

*Variables are assigned values by use of the assignment operator. num = 10 num = num + 1

COMMENTS:-

Anything related to code or not user can put with some special character is called comment.

Comments are of two types :-

Single-line comments:- Begins with the hash character ("#") and are terminated by the end of line.

Multi line comments:- Begins with """ comments """ .

Python ignores all text that comes after # to end of line for single line comment and """ """ for multi line comments.

LITERALS:- A literal is a sequence of one or more characters that stands for itself

There are two types of literal-

Numeric literal:- A numeric literal is a literal containing only the digits 0–9

If it contains integer value we use "int" and if it contains decimal values we use "float"

String literal:- String literals, or “strings,” represent a sequence of characters.

String literals may be surrounded by a matching pair of either single (') or double (") quotes

Denoted by "str" .

Thank you

Did you find this article valuable?

Support Harsh Chaudhary by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this