Topics Discussed:
- Features of Python
- History of ‘Python’
- Installation of Python
- Python Code Execution
- Different Implementations of Python
- Python Shell
- IDE
- How to Run Python code
- Help()
- Comments
- Statement
- Indentation and DocStrings
- OOPS in python
- Variables and Constants
- Data Types Introduction
- Functions
- Return Statement
Features of Python
• Code readability and indentation.
• Multiple programming paradigms like Procedural,oops & Functional programming.
• Dynamically-typed and garbage-collected.
• Case sensitive.
• Platform Independent.
• Powerful packages.
• Can extended python functionality by attaching libraries from different languages.
• Supports embedding to other languages.
• Freeware, Open source, copyrighted under a GPL-compatible license.
PyPI
• Python Package Index.
• The Python Package Index (PyPI) is a repository of software for the Python programming language.
• PyPI helps you find and install software developed and shared by the Python community.
• Package authors use PyPI to distribute their software
PIP
• pip is a de facto standard package-management system used to install and manage software packages written in Python
Byte Code
• Byte code is a lower-level, and platform-independent , representation of our source code.
• One of the language that uses the concept of Byte code is python.
IDE
• IDE is the Integrated Development Environment that provides the user interface for code development, testing and debugging features.
• A software application which contains development tools such as text editors, code libraries, compilers, and test platforms .
HELP ( help() )
• If no argument is passed, Python’s help utility (interactive help system) starts on the console.
• >>> help()
Python Statement
• Instructions that a Python interpreter can execute are called statements.
• var1=‘PYTHON’
• This is an assignment statement
Multi-line statement
• (Explicit)
• In Python, the end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\).
• Implicit.
• (),[],{}.
• We can also put multiple statementsin a single line using semicolons, as follows:
• Var1=“learn”; var2=“python”.
Python Identifiers
• An identifier is a name given to entities like class, functions, variables, etc.
• It helps to differentiate one entity from another.
• Identifiers can be a combination of letters in lowercase (a to z) or uppercase (AtoZ) or digits (0 too 9) or an underscore _.
• The 1st character can’t be a number.
• Python has a set of keywords that are reserved words.
• keywords cannot be used as identifiers.
Keywords
• In python3 there are 33 keywords
• Reserved words can’t be used as identifiers for other programming elements like name of variable, function etc.
• Eg: True,False,None
• Chk keyword.py
OOPS
• Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.
Objects
• In Python, everything is treated as an object.
• Every object has three attributes:
• Identity – This refers to the addressthat the object refers to in the computer’s
memory. Id()
• Type – This refers to the kind of objects that is created. Strings, integers etc.
(runtime). type()
• Value – This refers to the value stored by the object. List=[1,2,3] would hold the numbers 1,2 and 3 Variables
• A python ‘name’ can change its type.
• A single Python object can have lots of names.
• Single assignment.
• var1=5.
• Multiple Assignment .
• Python allows usto assign one single value to several variables at the same time
• var1=var2=var3=var4=‘Python’.
• Assign multiple objects to multiple variables.
• var1,var2,var=1,
Python’,3+4J.
• Chk(var.py) Constants
• A constant is a type of variable whose value cannot be changed
• In Python, constants are usually declared and assigned in a module.
• The module is a new file containing variables, functions, etc which is imported to the main file. Data
Types in Python
• Numeric data types: int, float, complex
• String (text)data type: str
• Sequence types: list, tuple, range
• Binary types: bytes, bytearray
• Mapping data type: dict
• Boolean type: bool
• Set data types: set, frozenset
• None data type
Objects of built-in type that are mutable
• Lists.
• Sets.
• Dictionaries.
• User-Defined Classes Objects of built-in type that are Immutable
• Numbers(Integer, Float, Complex )
• Booleans.
• Strings.
• Tuples.
• Frozen Sets.
• User-Defined Classes (It purely depends upon the user to define the characteristics).
Type Conversion in Python
• Python has two types of type conversion.
• Implicit Type Conversion.
• Explicit Type Conversion.
Implicit Type Conversion
• In Implicit type conversion of data types, the Python interpreter automatically converts one data type to another without any user involvement.
Explicit Type Conversion
(or)Type Casting
• Explicit Type conversion is also know as Type casting.
• The user usestype conversion functions (constructor functions) for converting purposes.
• syntax for such conversion will be:
(desired_datatype)(expression).
Strings
• In Python3, a string is a sequence of Unicode characters enclosed in ‘single’ or “ double” quotes.
• A character is simply a symbol.
Multiline Strings
• Multiline strings can be created using triple quotes
string data type
• String data types are represented by the keyword str.
• String data types in python are surrounded by either single, or double quotation marks.
• No character data type in Python
Indexing a string
• The index in Python returns the position of the element in the specified list or the Characters in the string.
• syntax is same for list or a string.
• The reason being that a string in Python is considered as a list of characters
starting from the index 0
• print(Str[0])
• print(Str[-1])
• Chk(string_index.py)
Escape Characters
• Code Result
• \’ Single Quote
• \” Double Quote
• \\ Backslash
• \n New Line
• \r Carriage Return
• \t Tab
• \b Backspace
• \f Form Feed
• \N{name} Prints a character from the Unicode database
• \ooo Octal value
• \xhhHex value
None data type
• There is no null but there is None.
• we can not assign a null value to the variable, and if you do, then it is illegal, and it will raise a SyntaxError.
• The following is not valid
• data = null
• print(data)
Numeric Data Type
• A number is an arithmetic entity that lets us measure something.
• Numeric data types are int, float and complex.
Int
• int data type can hold signed integers.
• Chk(int.py)
Float
• float data types in Python supports floating point real value.
• A float value is only accurate upto 15 decimal places. After that, it rounds the number off.
• Chk(float.py)
Operators
• Operators are special symbols in Python that carry out arithmetic or logical computation.
Arithmetic Operators
• + addition.
• -subtraction.
• * multiplication.
• / division.
• ** exponentiation.
• //integer division.
• % modular (remainder).
• Chk(cal.py)
Comparison operators
• Comparison operators are used to compare values. It returns either True or False according to the condition.
• > Greater than – True if left operand is greater than the right
x > y
• < Less than – True if left operand is less than the right
x < y
• == Equal to – True if both operands are equal
x == y
• != Not equal to – True if operands are not equal
x != y
• >= Greater than or equal to – True if left operand is greater than or equal to the right
Logical operators
• Logical operators are the and, or, not operators.
• and True if both the operands are true
x and y
• or True if either of the operands is true
x or y
• not True if operand is false (complements the operand)
not x
Bitwise operators
• Bitwise operators act on operands as if they were strings of binary digits.
• They operate bit by bit, hence the name.
• & Bitwise AND x & y
• | Bitwise OR x | y
• ~ Bitwise NOT ~x
• ^ Bitwise XOR x ^ y
• >> Bitwise right shift x >> 2
• << Bitwise left shift x << 2
Assignment operators
• Assignment operators are used in Python to assign values to variables
• = x = 5 x = 5
• += x += 5 x = x + 5
• -= x -= 5 x = x – 5
• *= x *= 5 x = x * 5
• /= x /= 5 x = x / 5
• %= x %= 5 x = x % 5
• //= x //= 5 x = x // 5
• **= x **= 5 x = x ** 5
• &= x &= 5 x = x & 5
• |= x |= 5 x = x | 5
• ^= x ^= 5 x = x ^ 5
• >>= x >>= 5 x = x >> 5
• <<= x <<= 5 x = x << 5
Boolean Data Type
• In Python, boolean variables are defined by the True and False keywords.
• Integer value ‘0’ is False.
• Any non zero value is considered as True.
• a = True
• type(a)
• class ‘bool’
• >>> b = False
• >>> type(b)
• class ‘bool’