Introduction to Python 3
Table of content
Introduction
Python is an interpreted high-level general-purpose programming language. Python was invented by Dutch programmer Guido van Rossum in the late 1980s.
Python is interpreted
Python source code is processed at runtime by the interpreter. We do not need a compiler to compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive
Python has an prompt (interactive terminal) with which you can interact
with the interpreter directly by writing your code.
Python is Object-Oriented
It supports Object-Oriented programming that encapsulates behavior and state within objects.
Python has easy learning curve
Python has simple structure, and a well defined syntax. It is easy to read and maintain. Hence a developer friendly programming language.
Python has broad standard library
Python has a broad set of the library. It is very portable and has cross-platform compatible with other hardware and operating system platforms. It can integrate with low-level modules. It provides interfaces to connect almost all the types databases. It has support for GUI library.
Python is functional
Other than OOP it also has strong construct to support functional
programming.
Python is dynamic
Python is not a static type language, the variable type is inferred dynamically at the runtime based on the value it holds. Duck typing system.
Garbage collection
Python has inbuilt support for automatic garbage collection.
Setup
Download and follow the instruction to install python 3 from the official site
https://www.python.org/downloads/
Install one of your favourite IDE, I usually go with pycharm CE or Visual
Studio Code. These softwares are easy to install.
https://www.jetbrains.com/pycharm/download/#section=mac
https://code.visualstudio.com/
Interactive terminal
Interactive terminal allows you to write, run, test and debug python code in an interactive way.
To open the interactive terminal, type python3 in the command prompt
or terminal.
I had python 2.x installed which had alias as python to launch in the terminal previous. When I when installed python 3, it cleared a new alias python3 pointing to python 3. You may need to type python in the terminal if you don't have python 2.x previously installed.
Read more on here on it: https://stackoverflow.com/questions/64801225/python-or-python3-what-is-the-difference
Print in Python
Let's interact in the terminal and print Hello World!, Python provide a print function to print information to standard I/O.
Comment in Python
Comments in Python begin with # symbol. Any character after the # symbol are part of the comment.
In the below example you observe the math calculation 2+3 which prints 5, but with # symbol no computation takes place.
Comments are useful to document the code in an application.
Variables
Variables are containers for storing data values. Python has no command for
declaring a variable.
A variable is created the moment you first assign a
value to it. Variables do not need to be declared with any particular type,
and can even change type after they have been set.
Below variables declared a, b, c.
We can also declare multiple variables in one line as declared below x,y, z
variables.
Casting
If you want to specify the data type of a variable, this can be done with casting.
Inbuilt data structures
Python has four basic inbuilt data structures that are Lists, Dictionary,
Tuple and Set. These are also Abstract data types (ADT).
List
List is a linear data structure. It can be used for any type of object, like numbers, strings also lists. Internally they are implemented as C arrays inside the Python interpreter and so they are just like an array of pointers.
List is declared using square brackets []. List is a heterogeneous data
structure, it can hold values of different types like integer, string and
float [1, 'Hello', 4.3] .
List functions
sort() ---> sorts the list in ascending order, it mutates the original
list
type(list) ---> type function returns the type of an object
append(value)
---> append() function adds a single element to a list, it mutates the
original list
extend([]) ---> extend function adds multiple elements
(another list) to a list, it mutates the original list
index(value)
---> index function returns the first appearance of the specified value
max(list)
---> max function returns an item from the list with max value
min(list)
---> min function returns an item from the list with min value
len(list)
---> len function returns the total length of the list
list(tuple)
---> list function converts a tuple into a list
Tuple
A Tuple is a collection of objects separated by commas. It can be used for any type of object, like numbers, strings also lists.
Tuple is similar to a list in terms of indexing, nested objects and repetition but it is different from list i.e. tuple is immutable while lists are mutable.
Tuple are declared using round brackets ().
List in tuple
The tuple value at 0th position is pointing to list at address x0000, when we append(6) to list, we are not changing the address pointed by tuple value 0th position.
When we try to modify the tuple value at 0th position using tup[0] = 0, we are
assigning a new value 0th address.
Dictionary is a key-value pair collection. Each key is separated from its
value by a colon (:) and the items are separated by commas. Dictionary is
declared using curly brackets {}.
The keys are unique in dictionary while values may not be. The values int the dictionary can be of any type, but the keys must be of an immutable type such as strings, numbers, or tuples.
Dictionary functions
dict.clear() ---> clean function removes all elements of dictionary dict
dict.copy()
---> copy function returns a shallow copy of dictionary dict, it returns a new copy
dict.keys() ---> keys function returns list of dictionary dict's keys
dict.get(key, default=None)
---> For key key, returns value or default if key not in dictionary
dict.items()
---> items function returns a list of dict's (key, value) tuple pairs
dict.setdefault(key,
default = None) ---> Similar to get(), but will set dict[key] = default if
key is not already in dict
dict.update(dict2) ---> update function adds dictionary
dict2's key-values pairs to dict
dict.values() ---> values function returns list of
dictionary dict's values
Set
Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python's set class represents set data structure. Internally the set is implemented as hash table or dictionary.
Thank you folks.