Python Unit1
Python Unit1
Python Unit1
Installa on:
Keywords Description
This is a logical operator which returns true if both the operands are
and
true else returns false.
Else is used with if and elif conditional statements. The else block is
else
executed if the given condition is not true.
Keywords Description
Identifiers:
Python Identifier is the name we give to identify a variable, function,
class, module or other object. That means whenever we want to give
an entity a name, that's called identifier.
Identifier is a user-defined name given to a variable, function, class,
module, etc.
Variables
A python variable is a symbolic name that is reference or pointer to an
object. Once an object is assigned to a variable, you can refer to the
object by that name. But the data itself is s ll contained within the
object.
The most commonly used methods of construc ng a mul word
variable name are the following three examples.
1. Camel case
Ex:- mightyMechanicalEngineering
2. Pascal case - Iden cal to camel case except the first word is also
capital
Ex:- MightyMechanicalEngineering
3. Snake case – mighty_mechanical_engineering
Comments:
Single line comments
Mul line comments
Indenta on:
Indenta on means spaces at the beginning of code line.
Python uses indenta on to indicate a block of code.
In other languages the indenta on in code is only for readability, the
indenta on in python is very imp.
Python will give error if you skip the indenta on. No of spaces is upto
programmer. But it should at least one.
Note:
We should use same no of spaces for all lines in same block of code.
No of spaces is user choice. Even one space, it will work
What is Statement in Python
A Python statement is an instruction that the Python interpreter can execute.
There are different types of statements in Python language as Assignment
statements, Conditional statements, Looping statements, etc. The token character
NEWLINE is used to end a statement in Python. It signifies that each line of
a Python script contains a statement. These all help the user to get the required
output.
Constants:
In Python, constants are usually declared and assigned in a module. Here, the
module is a new file containing variables, func ons etc. which is imported to the
main file.
Inside the module constants are wri en in all CAPITAL_LETTERS and underscores
separa ng the words.
Example:
1. Create a file name as constantmechc.py
2. PI=3.14, GRAVITY=9.81
3. Create a main.py (Program File)
Import constantmechc
print(constantmechc.PI)
print(constantmechc.GRAVITY)
Literals:
Literals means a constant
Generally, literal are a nota on for represen ng a fixed value in source
code. They can also be defined as raw value or data given in variables or
constants.
#Numeric Literals
Here, 20,24.5, 2+3j are considered as literals.
Python has different types of literals
1. String literals
2. Numeric Literals
3. Boolean Literals
4. Literal Collec ons
5. Special literals
6. Complex Literals
DATA Types:
For user defined modules refer constants topic, which is already discussed
above.
>>>help(math) to get more details about the math module in python
>>>import sys
>>>sys.path
>>>import from math as mt
>>>mt.pi
Operators: Operator is used to perform opera ons on data
Types of operators:
Arithme c operators,
Comparison operators,
Logical operators,
Bitwise operators,
Assignment operators,
Iden ty operators,
Membership operators
Assignment Operator:
Arithme c operators:
It is used to perform the mathema cal opera ons.
+ is for addi on
- is for subtrac on
* is for mul plica on
/ is for division
// floor division
% modula on
Arithme c – Assignment Operators:
Rela onal Operators: Rela onal operator checks a condi on and return
Boolean value. (True/False) < (Less than), > (Greater Than)
10<5
False
5<10
True
Logical Operators: A logical operator checks mul ple condi ons at a me and
return bool value. (True or False)
‘and’ logical operator: It will return True, when all the given condi on are
True otherwise it returns False.
‘or’ logical operator: It will return True, when at least one condi on is True
within given condi ons. Otherwise, it will return False.
Bitwise operators:
In Python, bitwise operators are used to perform bitwise calculations on
integers. The integers are first converted into binary and then operations are
performed on each bit or corresponding pair of bits, hence the name bitwise
operators. The result is then returned in decimal format.
Bitwise
~ NOT
inverts individual bits ~x
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise or operator Returns 1 if either of the bit is 1 else 0.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Name Space and Scope:
What is Scope?
1. Local Scope:
Contains names defined inside the current func on.
2. Global Scope:
Contains names defined at the top level of the script or module
3. Enclosing Scope:
Contains names defined inside any and all enclosed func on.
4. Built-In Scope:
Contains names built in to the python language.
Modifying the global variable in local scope:
We can’t modify global variable inside the local scope: