Introduction to Modern Python
Tags: beginners, python101, opensource
Published: 2022-02-12 15:54:03

Wikipedia states that the Python programming language is an interpreted high-level general-purpose programming language that is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including procedural object-oriented and functional programming.

## Getting Started with Python
Getting started with Python is as easy as downloading and installing the. Python interpreter
Download the latest stable release for your operating system from https://www.python.org/downloads/
After installing run the python interpreter from the terminal by typing the word python

If successful you should get something likes shown below;
Python 3.9.4 (tags/v3.9.4:1f2e308, Apr  6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information
>>>
Introduction to python program structure

## Input/Output (I/O) in python

The input() and print() functions are used for accepting and displaying data on the terminal respectively.
Accepting user input using input() function and converts it into a string. To change value use typecasting

>>>name = input(‘Enter name’)
>>>print(“Your name is: " + name)

To convert input to integer, float, list, tuple etc. Use the following examples i.e. casting

>>>num = int(input("Enter a number:"))
>>>num =float(input("Enter number "))
>>>li =list(input("Enter number "))
>>>num =tuple(input("Enter number "))`

## Variables and Datatypes
Variables can store data of different types, and different types can do different things

## Operators
	
The operators **+, -, *** and **/** work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping
Division (**/**) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the **//** operator; to calculate the remainder you can use %:
>>> 17 / 3  # classic division returns a float
5.666666666666667
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division

**Operator**		**Name**		**Example**	

- ==			Equal				x == y	
- !=			Not equal			x != y	
- >			Greater than			x > y	
- <			Less than			x < y	
- >=			Greater than or equal to	x >= y	
- <=			Less than or equal to		x <= y
**Operator**	**Description**			**Example**

- and Returns True if both statements are true x < 5 and  x < 10	
- or Returns True if one of the statements is true x < 5 or x < 4	
- not Reverse the result, returns False if the result is true	not(x < 5 and x < 10)

## Lists, Tuples, Sets and Dictionaries
There are four collection data types in the Python programming language:

1. List is a collection which is ordered and changeable. Allows duplicate members.
2. Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
3. Set is a collection which is unordered and unindexed. No duplicate members.
4. Dictionary a set of key: value pairs, with the requirement that the keys are unique (within one dictionary) It is ordered* and changeable. No duplicate members. (*for python 3.7 and above)

- fruits= [‘Apple’, ’Mango’, ’Banana’]	#List
- fruits =(‘Apple’, ’Mango’, ’Banana’)	#Tuple
- basket = {'orange', 'apple', 'pear', 'banana'}#Set
- a = {‘name’:’John’, ’surname’:’Ford’}	#Dictionary


## Conditionals
>>>if b > a:
 	print("b is greater than a")
>>>if b > a:
	  print("b is greater than a")
	 elif a == b:
		print("a and b are equal
>>>if b > a:
  print("b is greater than a")
	elif a == b:
  	print("a and b are equal")
	else:
  	print("a is greater than b")

- There can be zero or more elif parts, and the else part is optional. 
- The keyword elif is short for ‘else if’, and is useful to avoid excessive indentation. 
- An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.

## Loops
**`for` statement**
Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence
>>> `# Measure some strings:`
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print(w, len(w))
...
cat 3
window 6
defenestrate 12

**`While` loop**
>>>count = 0
>>>while (count<5):
	count = count + 1
	print("Hello there!")

## Functions

- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result.
- You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
The syntax for a user defined function is as follows using the `def` keyword;
def function_name(parameters):
	#some code
	return expression

A function is invoked by declaring it as follows;
`function name(parameters)`


>>> def fib(n):    # write Fibonacci series up to n
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
>>> `# Now call the function we just defined:`
... fib(2000)

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

## Classes

- Almost everything in Python is an object, with its properties and methods.
- A Class is like an object constructor, or a "blueprint" for creating objects.
 class MyClass:  #class named MyClass, with a property named x
     x = 5
p1 = MyClass() #object named p1, and print the value of x
print(p1.x) # print the value of x

**`__init__()` function**
All classes have a function called `__init__()`, which is always executed when the class is being initiated. The `__init__()` function is called automatically every time the class is being used to create a new object.
Use the `__init__()` function to assign values to object properties, or other operations that are necessary to do when the object is being created
`class Person:`
 



 def `__init__`(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Visit my article [Introduction to Data Structures and Algorithms with Python](https://dev.to/mmk2020/introduction-to-data-structures-and-algorithms-with-python-175j) to get familiar with data structures and algorithms. Understanding **D**ata **S**tructures and **A**lgorithms (DSA) will help you write effective and efficient programs that work optimally under given conditions in terms of speed (execution time) and memory usage.


_sources;
en.wikipedia.org
w3schools.com_