Python Notes
Python Notes

Python Notes

Tags
python
OOP
Published
December 15, 2021

What is Python?

Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically-typed and garbage-collected.

Features

  • Used in Machine Learning (TensorFlow, PyTorch)
  • Used for Automation (Selenium, Puppet)
  • Used for Building Full-stack apps (Django, Flask)

Data-Types in Python

Data Type
Example
Text Type
str
Numeric Types
int , float, complex
Sequence Types
list, tuple, range
Mapping Type
set, frozenset

String

A string is a series of characters, surrounded by single, double or triple single/double quotes.
For single line strings we can use single/double quotes. ('...'), (”...”)
For multi-line strings we use triple single/double quotes. ("""..."""), ('''...''')
single_quote_string = 'Hello World' double_quote_string = "Hello World" triple_quote_string = ''' Hello World '''
Examples of Strings

Numeric Types

  • int - Integer are zero, positive or negative whole numbers without a fractional part and having unlimited precision.
a = 69
Integer
  • float - Float are positive and negative real numbers with a fractional part denoted by the decimal symbol . or the scientific notation E or e .
a = 420.69
Float
  • complex - Complex number is a number with real and imaginary components. For example, is a complex number where 4 is the real component and 10 multiplied by j is an imaginary component.
a = 4 + 10j
Complex Number

Sequence Types

  • list - A list stores a series of items in a particular order. Lists allow you to store sets of information in one place, whether you have just a few items or millions of items. And it’s mutable. It is denoted by square brackets [].
l = []
  • tuple - Tuples are like list but they are immutable.
t = ()
  • range - Range is use to create sequence of number. It was a built-in function that returned a list in Python 2 but in Python 3 it’s a datatype.
type(range(5))

Mapping Types

  • set - Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. It is denoted by curly brackets {}.
s = {1,2,3,4,5,6}
  • frozenset - Frozenset is datatype which had property of sets but are immutable like tuple.
s = {1,2,3,4,5,6} frozen_s = frozenset(s)

Print in Python

In python print keyword is used to print a value or string.
print("Hello World")

Conditional Statements

if statement

ifstatements are used to test for particular conditions and respond appropriately. If the condition is true it get executed.
n = 1 if (n==1): print("N is one")
if statement

if-else statement

if-else works same as if statement but when the condition is false it executes the else part.
n = 0 if (n==1): print("N is one") else: print("N is not one")
if-else statement

if-elif-else statement

if-elif-else checks multiple condition and executes and breaks if the statements is found true.
n = 0 if (n==1): print("N is one") else: print("N is not one")
if-elif-else statement

Looping Statements

for loop

The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects.
for variable in squence: statements
for loop

while loop

The while loop is used to iterate over a block of code as long as the test expression (condition) is true.
while condition: statements

Regular Expression

Regular Expression also know as Regex is use to search pattern in given string.
import re
Using Regex in python

Special Characters

^ | Matches the expression to its right at the start of a string. It matches every such instance before each \n in the string.
$ | Matches the expression to its left at the end of a string. It matches every such instance before each \n in the string.
. | Matches any character except line terminators like \n.
\ | Escapes special characters or denotes character classes.
A|B | Matches expression A or B. If A is matched first, B is left untried.
+ | Greedily matches the expression to its left 1 or more times.
. | Greedily matches the expression to its left 0 or more times.
? | Greedily matches the expression to its left 0 or 1 times. But if ? is added to qualifiers (+, *, and ? itself) it will perform matches in a non-greedy manner.
{m} | Matches the expression to its left m times, and not less.
{m,n} | Matches the expression to its left m to n times, and not less.
{m,n}? | Matches the expression to its left m times, and ignores n.

Special sequence

\w | Matches alphanumeric characters, which means a-z, A-Z, and 0-9. It also matches the underscore, _.
\d | Matches digits, which means 0-9.
\D | Matches any non-digits.
\s | Matches whitespace characters, which include the \t, \n, \r, and space characters.
\S | Matches non-whitespace characters.
\b | Matches the boundary (or empty string) at the start and end of a word, that is, between \w and \W.
\B | Matches where \b does not, that is, the boundary of \w characters.
\A | Matches the expression to its right at the absolute start of a string whether in single or multi-line mode.
\Z | Matches the expression to its left at the absolute end of a string whether in single or multi-line mode.

Sets

[ ] | Contains a set of characters to match.
[amk] | Matches either a, m, or k. It does not match amk.
[a-z] | Matches any alphabet from a to z.
[a\-z] | Matches a, -, or z. It matches - because \ escapes it.
[a-] | Matches a or -, because - is not being used to indicate a series of characters.
[-a] | As above, matches a or -.
[a-z0-9] | Matches characters from a to z and also from 0 to 9.
[(+*)] | Special characters become literal inside a set, so this matches (, +, *, and ).
[^ab5] | Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5.

Groups

( ) | Matches the expression inside the parentheses and groups it.
(? ) | Inside parentheses like this, ? acts as an extension notation. Its meaning depends on the character immediately to its right.
(?PAB) | Matches the expression AB, and it can be accessed with the group name.
(?aiLmsux) | Here, a, i, L, m, s, u, and x are flags:
  • a β€” Matches ASCII only
  • i β€” Ignore case
  • L β€” Locale dependent
  • m β€” Multi-line
  • s β€” Matches all
  • u β€” Matches unicode
  • x β€” Verbose
(?:A) | Matches the expression as represented by A, but unlike (?PAB), it cannot be retrieved afterwards.
(?#...) | A comment. Contents are for us to read, not for matching.
A(?=B) | Lookahead assertion. This matches the expression A only if it is followed by B.
A(?!B) | Negative lookahead assertion. This matches the expression A only if it is not followed by B.
(?<=B)A | Positive lookbehind assertion. This matches the expression A only if B is immediately to its left. This can only matched fixed length expressions.
(?<!B)A | Negative lookbehind assertion. This matches the expression A only if B is not immediately to its left. This can only matched fixed length expressions.
(?P=name) | Matches the expression matched by an earlier group named β€œname”.
(...)\1 | The number 1 corresponds to the first group to be matched. If we want to match more instances of the same expression, simply use its number instead of writing out the whole expression again. We can use from 1 up to 99 such groups and their corresponding numbers.

re Functions

re.findall(A, B) | Matches all instances of an expression A in a string B and returns them in a list.
re.search(A, B) | Matches the first instance of an expression A in a string B, and returns it as a re match object.
re.split(A, B) | Split a string B into a list using the delimiter A.
re.sub(A, B, C) | Replace A with B in the string C.

Installing Packages in Python

You can find python packages using The Python Package Index (PyPI) and installing using package manager of your choice such as pip, anaconda, poetry etc.
Reference: docs.python.org