Installation Instructions:: - Linux
Installation Instructions:: - Linux
Installation Instructions:: - Linux
-Linux: Python comes preinstalled with Linux, so the procedure should be simple.
Follow the instructions at this link to update to the latest version:
https://docs.python-guide.org/starting/install3/linux/
At the end, you can also choose to use an alias, which makes things easier.
Enter the following command in your terminal: alias python=python3
-Mac: Macs come preinstalled with Python 2.7, which sadly, will reach the end of
it's life on
01/01/2020.
Follow these instructions at this link to install Python3:
https://programwithus.com/learn-to-code/install-python3-mac/
After installing Python, you’re going to need to install pip, to be able to install libraries
without having to always clone the github repo.
-Linux: Open your terminal and type: sudo apt install python3-pip
-Mac: If you followed the instructions above, pip should be installed already.
-Windows: If you followed the instructions above, pip should be installed already.
To check if pip is installed, open terminal/command prompt and type: pip --version
On Mac, type pip3 --version
If any doubts go to- https://stackoverflow.com/questions/4750806/how-do-i-install-
pip-on-windows
Python Fundamentals
Python character Set
• lower ⇐ a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z
• upper ⇐ A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
• digit ⇐ 0|1|2|3|4|5|6|7|8|9
• ordinary ⇐ |(|)| [ | ] | { | } |+|-|*|/|%|!|&| | |~|^||,|.|:|;|$|?|#
• graphic ⇐ lower | upper | digit | ordinary
• special ⇐ ’ | " | \
• white space ⇐ | → | ←- (space, tab, or newline)
Expressions
An expression is any legal combination of symbols that represents a value.
e.g., a = 15
Statements
• Instructions that a Python interpreter can execute are called statements.
• A statement is a programming instruction that does something i.e., some action takes
place.
e.g.
a=10
print(a)
Comments
• Comments are the additional readable information to clarify the source code
• Comments are ignored by the python interpreter but it can be read by the programmer.
Comments in Python begin with symbol # or „‟‟ .
• Comments enclosed in triple quotes are called docstrings.
Single Line Comment:
In Python, we use the hash (#) symbol to start writing a comment.
#This is a comment
#print out Hello
print('Hello')
Multi-line comments
Another way of doing this is to use triple quotes, either ''' or """.
"""This is also a
perfect example of
multi-line comments"""
print(“Hello”)
Functions
A function is a code that has a name and it can be reused by specifying its name in the
program, where needed.
drink = “WATER"
food = “EAT”
Sit = “TABLE”
def menu(x):
if x == drink:
print(drink);
print(“Prints only if condition is TRUE”);
else:
print(food);
print(“Prints only if condition is FALSE”);
print(sit)
menu(drink)
Blocks and Indentation
BLOCKS
A group of statements which are part of another statement or a function are called
block or code block or suite in Python.
Example
def menu(x):
if x == drink:
print(drink); block
print(“Prints only if condition is TRUE”);
else: block
print(food); block
print(“Prints only if condition is FALSE”);
print(sit);
menu(drink)
INDENTATION
Printing variable
a = 10
print(a);
PYTHON INPUT
In Python input() functions read data from keyboard as string,
irrespective of whether it is enclosed with quotes ('' or "" ) or not.
Taking input from keyboard at runtime
age=input(“Enter you age:”)
print(“Your age =”,age);
input() function always returns a value of string type.
To convert the string value into integer type python offers two functions
int() and float(). Both functions are used to convert string type input to
integer type.
Example 1:
Name = input(“What is your name : ”)
print(Name);
Example 2:
A = input(“Enter First number : ”)
B = input(“Enter Second number : ”)
A = int(A)
B = int(B)
C = A + B
print(C);
We can also write the above program as
A = int(input(“Enter First number : ”))
B = int(input(“Enter Second number : ”))
C = A + B
print(C);
Tokens or Lexical Unit
The smallest individual unit in a program is known as a Token or a lexical unit.
Python has following tokens:
Keywords
Keywords are words that convey a special meaning to the language compiler/interpreter.
These are reserved for purpose and must not be used as normal identifier names.
as elif if or yield
>>> global = 1
File "<interactive input>", line 1
global = 1
^
SyntaxError: invalid syntax
>>> a@ = 0
File "<interactive input>", line 1
a@ = 0
^
SyntaxError: invalid syntax
Literals
Literals are data items that have a fixed value
String Literals
String literals can be formed by enclosing a text in the quotes. We can use both single as
well as double quotes for a String.
Eg:
"Aman" , '12345'
Types of Strings:
There are two types of Strings supported in Python:
a) Single line String- Strings that are terminated within a single line are known as Single
line Strings.
>>> text1='hello'
b) Multi line String- A piece of text that is spread along multiple lines is known as
Multiple line String.
There are two ways to create Multiline Strings:
1). Adding black slash at the end of each line.
>>> text1='hello\
user'
>>> text1
'hellouser'
>>>
2).Using triple quotation marks:-
>>> str2='''welcome
to
SSSIT'''
>>> print (str2)
welcome
to
SSSIT
>>>
Numeric Literals
Numeric Literals are immutable. Numeric literals can belong to following four different
numerical types.
Numbers( can be Integers of Real numbers with In the form of a+bj where a
both positive and unlimited size both integer and forms the real part and b forms
negative) with no followed by fractional part eg: - the imaginary part of complex
fractional part.eg: lowercase or 26.2 number. eg: 3.14j
100 uppercase L eg:
87032845L
Boolean Literals
A Boolean literal can have any of the two values: True or False.
Special Literals
Python contains one special literal i.e., None.
None is used to specify to that field that is not created. It is also used for end of lists in
Python.
Operators
Types of Operator
Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
- Subtraction Subtracts right hand operand from left hand operand. a–b=-
10
% Modulus Divides left hand operand by right hand operand and returns b%a=0
remainder
// Floor Division - The division of operands where the result is the 9//2 = 4
quotient in which the digits after the decimal point are removed. and
But if one of the operands is negative, the result is floored, i.e., 9.0//2.0 =
rounded away from zero (towards negative infinity) − 4.0, -11//3
= -4, -
11.0//3 =
-4.0
Python Comparison Operators
These operators compare the values on either sides of them and decide the
relation among them. They are also called Relational operators.
Assume variable a holds 10 and variable b holds 20, then −
<> If values of two operands are not equal, then condition (a <> b)
becomes true. is true.
This is
similar to
!=
operator.
> If the value of left operand is greater than the value of right (a > b)
operand, then condition becomes true. is not
true.
< If the value of left operand is less than the value of right (a < b)
operand, then condition becomes true. is true.
>= If the value of left operand is greater than or equal to the (a >= b)
value of right operand, then condition becomes true. is not
true.
<= If the value of left operand is less than or equal to the value of (a <= b)
right operand, then condition becomes true. is true.
+= Add AND It adds right operand to the left operand and assign the c += a is
result to left operand equivalent
to c = c +
a
/= Divide AND It divides left operand with the right operand and assign c /= a is
the result to left operand equivalent
to c = c /
ac /= a is
equivalent
to c = c /
a
//= Floor It performs floor division on operators and assign value c //= a is
Division to the left operand equivalent
to c = c //
a
& Binary AND Operator copies a bit to the result if it exists in (a & b)
both operands (means
0000 1100)
^ Binary XOR It copies the bit if it is set in one operand but not (a ^ b) =
both. 49 (means
0011 0001)
~ Binary Ones (~a ) = -61
Complement (means
1100 0011
in 2's
It is unary and has the effect of 'flipping' bits. complement
form due to
a signed
binary
number.
<< Binary Left The left operands value is moved left by the a << 2 =
Shift number of bits specified by the right operand. 240 (means
1111 0000)
>> Binary Right The left operands value is moved right by the a >> 2 =
Shift number of bits specified by the right operand. 15 (means
0000 1111)
and Logical If both the operands are true then condition becomes (a and b)
AND true. is true.
not Logical Used to reverse the logical state of its operand. Not(a
NOT and b) is
false.
Used to reverse the logical state of its operand.
Python Membership Operators
Python’s membership operators test for membership in a sequence, such as
strings, lists, or tuples. There are two membership operators as explained below
not in Evaluates to true if it does not finds a variable in the specified x not in y,
sequence and false otherwise. here not in
results in a 1
if x is not a
member of
sequence y.
Python Identity Operators
Identity operators compare the memory locations of two objects. There are two
Identity operators explained below −
1 **
Exponentiation (raise to the power)
2 ~+-
Complement, unary plus and minus (method names for the last two are +@
and -@)
3 * / % //
Multiply, divide, modulo and floor division
4 +-
Addition and subtraction
5 >> <<
Right and left bitwise shift
6 &
Bitwise 'AND'
7 ^|
Bitwise exclusive `OR' and regular `OR'
9 <> == !=
Equality operators
10 = %= /= //= -= += *= **=
Assignment operators
11 is is not
Identity operators
12 in not in
Membership operators
13 not or and
Logical operators
Punctuators
Punctuators are symbols that are used in programming language to organize sentence
structures and indicate the rhythm and emphasis of expressions, statements and program
structure.
Most common punctuators in python are- ' " # \ ( ) [ ] { } @ , : . =