Professional Documents
Culture Documents
Python Notes
Python Notes
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
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 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:
Whenever you are done in the python command line, you can simply type the following to quit
the python command line interface:
exit()
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.
• 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
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.
• 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.
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
• 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,
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)
+ 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:
= 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
== Equal x == y
!= Not equal x != y
x < 10
Not Reverse the result, returns False if the result is not(x < 5 and
true
x < 10)
is not Returns True if both variables are not the same x is not y
object
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left,