Python Mini Course - Course Notes: Parts 1 (Introduction) & 2 (Syntax) of 5

I am not the best at learning stuff unless there is a very good reason to learn it. I have been meaning to learn Python for a while but lacking sufficient motivation. Now I have a very good reason - I need to use Python within Mulesoft to develop Mulesoft Applications. Time to start learning! These notes come from some PDFs I stumbled across, and - on-first-glance - they looked like a very good beginners introduction to Python. I should point out that taking notes is the best way I learn. If I just read stuff, I tend to zone out and learn nothing.

1) Python Introduction

Python was developed by Guido van Rossum who started implementing Python in 1989.
Python is named after the comedy television show ‘Monty Python’s Flying Circus’.

To install Python on your operating system, go to:

Python is an interpreted programming language. You write Python (.py) files in a text editor and then put those files into the python interpreter to be executed. Example:

C:\Users\You> python helloworld.py

You can practice with Online IDE:

2) Python Syntax

2.1) Print Function

The print() function prints the specified message to screen or other standard output device.

print("Hello World")

A string is a collection of characters inside “Double quotes” or ‘Single quotes’ (you can use ‘Single quotes’ inside “double quotes” and vice versa.)

2.2) Escape Sequences and Raw String

To insert characters that are illegal in a string, use the escape character \ followed by the character you want to insert. For example:

print("Hello \"World\" World")

Image: Example: Hello "World" World (using onlinegdb)

More escape sequences used in Python:

\' = Single Quote
\" = Double Quote
\\ = Backslash
\n = New line
\t = Tab
\b = Backspace (delete the letter before ‘\b’)

Raw string notation (r"text") keeps regular expressions meaningful and confusion-free. Example:

print(r"Line A \nLine B")

Output:

Line A \nLine B

2.3) Comments

A hash sign (#) that is not inside a string literal begins a comment.

Python does not really have syntax for multi-line comments. Python will ignore string literals that are not assigned to a variable, so you can add a multiline string (triple quotes) in your code and place your comment inside it:

"""
This is a
multi-line comment
"""

2.4) Python Basic Operators

Python Arithmetic Operators:

+  | Addition
-  | Subtraction
*  | Multiplication
/  | Division
%  | Modulus
** | Exponent
// | Floor Division

Python Comparison Operators:

== | Is equal to
!= | Is not equal to
<> | Is not equal to (similar)
>  | Greater than
<  | Less than
>= | Greater than or equal to
<= | Less than or equal to

Python Assignment Operators (with equivalents):

=   | Simple assignment
+=  | Add AND assignment (c = c + a)
-=  | Subtract AND assignment (c = c - a)
*=  | Multiple AND assignment (c = c * a)
/=  | Divide AND assignment (c = c / a)
%=  | Modulus AND assignment (c = c % a)
**= | Exponent AND assignment (c = c ** a)
//= | Floor division AND assignment (c = c // a)

Python Logical Operators:

and
or
not

2.5) Python as a Calculator

Image: 4/2 = 2.0 and 4//2 = 2 (integer division)

Round Function

The round() function returns a floating-point number that is a rounded version of the specified number, with the specified number of decimals.

Syntax: round(number,digits)
number: Required - number to be rounded.
digits: Optional - number of decimals to use when rounding (default is 0)

Precedence Rule

To evaluate complex expressions, python follows the rule of precedence which governs the order in which the operation takes place.

Operator    | Precedence and Associativity rule
------------+----------------------------------
Parentheses | Highest
Exponential | Right to left
* / // %    | Left to right
+ -         | Left to right

2.6) Variable in Python

Creating Variables

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable (using = etcetera). Python has no command for declaring a variable. Variables do not need to be declared with any type and can even change type after they have been set. String variables can be declared either by using single or double quotes.

Rules for Python Variable Names

- must start with a letter or the underscore character
- cannot start with a number
- can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
- are case-sensitive

Assign Value to Multiple Variables

Example:

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Orange
Banana
Cherry

x = y = z = "Orange"
print(x)
print(y)
print(z)

Orange
Orange
Orange

2.7) Basic Built-in Data Types

Python has various standard data types that are used to define the operations possible on them. Python standard data types:

Text type     : str
Numeric types : int, float, complex
Sequence types: list, tuple, range
Mapping type  : dict
Set types     : set, frozenset
Boolean type  : bool(True, False)
Binary types  : bytes, bytearray, memoryview

String (Str): A string value.
Integer (int):  Positive or negative whole numbers.
Float (float): Any real number with a floating-point representation.
Complex number (complex): A number with a real and imaginary component represented as x +yj where x and y are floats and j is the square root of -1 (imaginary number)
Boolean: True or False (Note: ‘T’ and ‘F’ are capitals. ‘true’ and ‘false’ are not valid Booleans.)
List []: An ordered collection of one or more data items, not necessarily of the same type.
Tuple (): An ordered collection of one or more data items, not necessarily of the same type.

Note: The main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable.

2.8) Getting the Data Type

Example:

print(type(1234))
print(type(55.50))
print(type(6+4j))
print(type("hello"))
print(type([1,2,3,4]))
print(type((1,2,3,4)))

Image: Output demonstrating type() from above example - int,float,complex,str,list,tupe

Comments