Introduction to Python Basics

Python, this amazing language with an incredible following and users, like Ruby is an interpreted language.

This means that there’s no compiler and there’s no prior compilation of source code into machine language to be run as in C or C++ programming language.

You write your python code and save the file with a .py extension. The python interpreter parses your .py file and interprets the code as it runs.

Python also comes with an interactive interpreter. The interactive interpreter helps the user to pratice and test code. It is a sandbox to experiment with Python.

Python Comments

A single-line comment in Python is denoted with the pound or hash symbol #

For example: # my_first.py - This is my first cool python program

A multi-line comments lie between “”” “”” quotation marks

For example:
"""
This is my Multi-line comment
which stays within these marks

"""

Python types and variables

---
In Java/C/C++
int x = 5000; # Type of variable x is an int

In Python
x = 5000     # No type declaration,  and no semicolon
---

** Variables in Python take on the type of object they are representing. Variables in Python are dynamically typed:

---
In your Python interpreter type:
>>> x = 5
>>> type(x)
<class 'int'>
The type of object represented here is clearly an int

>>> y = "Saturn"
>>> type(y)
<class 'str'>
The type of object represented here is clearly a str

>>> z = 88.0
>>> type(z)
<class 'float'>
The type of object represented here is clearly a float
---

Python has 3 numeric types: int, float and complex

---
In Python 3, the ‘/’ operator gives floating point results
>>> 6/3
2.0
>>> 6.0/2.0
3.0
>>> 6.0/2
3.0
>>> 6/2.0
3.0

Python Integer division '//' with no remainder, returns only int part
>>> 7 // 3
2

The modulo operator '%' returns the remainder as int
>>> 7 % 3
1

The exponential operator '**' returns int
>>> 5 ** 2
25

In Python 3, the ‘+’ operator gives floating point results adding int and float
>>> 1.0 + 5
6.0
>>> 1.0 + 5.0
6.0
---