Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

What is Python?

Python is a general-purpose interpreted, interactive, object-oriented, and high-level


programming language. It was created by Guido van Rossum, and released in 1991.
Python got its name from “Monty Python’s flying circus”.

It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.

What can Python do?


• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used for rapid prototyping, or for production-ready software development.

Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.
• Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
• Python can be treated in a procedural way, an object-oriented way or a functional way.

Python Features
• Easy-to-learn:Python is clearly defined and easily readable. The structure of the
program is very simple. It uses few keywords.
• Easy-to-maintain: Python's source code is fairly easy-to-maintain.
• Portable: Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
• Interpreted: Python is processed at runtime by the interpreter. So, there is no need
to compile a program before executing it. You can simply run the program.
• Extensible: Programmers can embed python within their C,C++,Java script ,ActiveX,
etc.
• Free and Open Source: Anyone can freely distribute it, read the source code,
and edit it.
• High Level Language: When writing programs, programmers concentrate
on solutions of the current problem, no need to worry about the low level details.
• Scalable: Python provides a better structure and support for large programs than
shell scripting.

Python Syntax compared to other programming languages


• Python was designed for readability, and has some similarities to the English language
with influence from mathematics.
• Python uses new lines to complete a command, as opposed to other programming
languages which often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to define scope; such as the scope of
loops, functions and classes. Other programming languages often use curly-brackets for
this purpose.

Python QuickStart
Python is an interpreted programming language, this means that as a developer you write
Python (.py) files in a text editor and then put those files into the python interpreter to be
executed.

The way to run a python file is like this on the command line:

C:\Users\Your Name>python helloworld.py

Whenever you are done in the python command line, you can simply type the following to quit
the python command line interface:

exit()

MODES OF PYTHON INTERPRETER:


Python Interpreter is a program that reads and executes Python code. It uses 2 modes of
Execution.
1. Interactive mode
2. Script mode

1. Interactive mode:
• Interactive Mode, as the name suggests, allows us to interact with OS.
• When we type Python statement, interpreter displays the result(s) immediately.

Advantages:
• Python, in interactive mode, is good enough to learn, experiment or explore.
• Working in interactive mode is convenient for beginners and for testing small pieces
of code.
Drawback:
• We cannot save the statements and have to retype all the statements once again to
re-run them.
• In interactive mode, you type Python programs and the interpreter displays the
result:
>>> 1 + 1
2
The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready for you to
enter code. If you type 1 + 1, the interpreter replies 2.
>>> print ('Hello, World!')
Hello, World!
This is an example of a print statement. It displays a result on the screen. In this case, the
result is the words.

2. Script mode:
• In script mode, we type python program in a file and then use interpreter to execute
the content of the file.
• Scripts can be saved to disk for future use. Python scripts have the extension .py,
meaning that the filename ends with .py
Integrated Development Learning Environment (IDLE):
• Is a graphical user interface which is completely written in Python.
• It is bundled with the default implementation of the python language and also comes with
ptional part of the Python packaging.

Features of IDLE:
Multi-window text editor with syntax highlighting.
• Auto completion with smart indentation.
• Python shell to display output with syntax highlighting.

The Python Character Set

• It is the set of valid characters that python understands. Python uses the Unicode
character set.
• It includes numbers from numbers like
Digits: 0-9,
Letters: a-z, A-Z
Operators: +,-,/,*,//,**
Punctuators like : (colon), (), {}, []
Whitespaces like space, tabs, etc
What are tokens?

Tokens are building blocks of a language. They are the smallest individual unit of a
program. There are five types of tokens in Python. They are Keywords, Identifiers,
Literals, Punctuators, and Operators.

Types of Tokens

Keywords

• Keywords are the pre-defined set of words in a language that perform their specific
function.
• You cannot assign a new value or task to them other than the pre-defined one.
• You cannot use them as a variable, class, function, object or any other identifier.
For example: if, elif, while, True, False, None, break etc

• These have their special task that you cannot change.


For example break will only end the loop you cannot make it start the loop. (We’ll be
covering loops in future lectures).
Identifiers

Now identifiers are the names that you can assign a value to. An identifier can be anything for
example,

1a = 10

Here, a is a valid identifier name. Any name you give your variable, function, or class is an
identifier of that particular thing. Now there are certain rules that you have to follow to define a
valid identifier name.

Rules for valid identifier name

• A valid identifier name can have letters, digits, and underscore sign.
• It can start with an alphabet or underscore but can never start with a digit.
• It can never be a keyword name.
• An identifier name can be of variable length.
• The only special symbol that can be used in identifier name is underscore( _ ).
• One more thing that you should remember that python is case sensitive i.e.,

1a = 10
2A = 5

These two hold two different values. a holds the value 10 and A holds the value 5.

Examples of valid identifier names include: a, _a, a12, etc


Examples of invalid identifier names include: 1a, $a, elif, print.
If you don’t understand why they are valid/invalid, read the rules again.

Literals
• Literals are the fixed or constant values. They can either be string, numeric or boolean.

• For example, anything within single or double quotes is classified as a string and is literal
because it is a fixed value i.e, “Coding Ground” is literal because it is a string.

• Another example is, 10. It is a simple number but is a fixed value. It is constant and it
will remain constant. You can perform operations like addition or subtraction but the
value of these two characters 1 and 0 put together in a correct order gives them a value
equal to ten and that cannot be changed.

• Boolean only consist of 2 values, True and False. Remember that “T” of True is capital.
Python is case sensitive and if you write True with small “t” like true it will hold a
different meaning. It will act as another variable.
Punctuators or Separators

• Punctuators, also known as separators give a structure to code.

• They are [mostly] used to define blocks in a program. We will be covering code
blocks in control flow statements when we discuss how to apply conditions in Python.
Some examples of punctuators include
single quotes – ‘ ‘ , double quote – ” ” , parenthesis – ( ), brackets – [ ], Braces – { },
colon – ( : ) , comma ( , ), etc.
Punctuators and operators go hand in hand are used everywhere. For example,

1name = "coding ground"

• Here, an assignment operator ( = ) and punctuator, (” “) is used.

Operators
Operators are the symbols which are used to perform operations between operands.
• Unary Operators: Operators having single operand.
Eg. +8, -7, etc
• Binary Operators: Operators working on 2 operands.
Eg. 2+2, 4-3, 8*9, etc
Similarly, there are Ternary Operators that work on 3 operands and so on.
• These are just basics and the operators listed below are very important
• Arithmetic operators (+, -, /, * etc)
• Assignment operators (=)
• Comparison operators (>, <, >=, <=, ==, !=)
• Logical operators (and, or, not)
• Identity operators (is, is not)
• Membership operators (in, not in)
• Bitwise operators (&, |, ^ etc)

Python Arithmetic Operators


• Arithmetic operators are used with numeric values to perform common mathematical
operations:

Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

//= x //= 3 x = x // 3

**= x **= 3 x = x ** 3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

Python Comparison Operators


Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Python Logical Operators


Logical operators are used to combine conditional statements:
Operator Description Example

and Returns True if both statements are true x < 5 and

x < 10

Or Returns True if one of the statements is true x < 5 or x < 4

Not Reverse the result, returns False if the result is not(x < 5 and
true
x < 10)

Python Identity Operators


Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:

Operator Description Example

is Returns True if both variables are the same object x is y

is not Returns True if both variables are not the same x is not y
object

Python Bitwise Operators


Bitwise operators are used to compare (binary) numbers:
Operator Name Description

& AND Sets each bit to 1 if both bits are 1

| OR Sets each bit to 1 if one of two bits is 1

^ XOR Sets each bit to 1 if only one of two bits is 1

~ NOT Inverts all the bits

<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost

bits fall off

>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left,

and let the rightmost bits fall off

You might also like