We can check which version of Python is installed on our machine using the command python --version
or python3 --version
. If the Python version is not displayed, then we will need to download and install Python from the official website.
We can access and use the Python's interactive version using the python
or python3
command in Terminal or Command Prompt or Window Powershell.
We can execute the Python Statements directly in the interactive version.
We can obtain the Copyright information using the copyright()
command.
We can obtain the information about credits using the credits()
command which which will give us the names of the companies which participated in Python Development.
We can come out of the interactive version using the exit()
command in the interactive version.
Some of the most common IDEs and Code Editors for Python Development are:
Anaconda is a free and open-source distribution of thye Python and R programming language for scientific computing, that aims to simplify package management and deployment. A lot of Python packages are pre-installed with this software.
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. The name Jupyter comes from the following programming languages: Julia, Python and R.
Advantages | Disadvantages |
Dynamic Typing | No type checking |
Fast Turnaround Time | Slow Performance |
Portability | Execution is dependent on the environment and language version |
Focused execution | Limited hardware control and Memory Management |
Easy to debug, simple functionality |
Advantages | Disadvantages |
Fast Execution speed | Hardware specific |
Easier access to hardware | Require compilation before execution |
Efficient at Memory Management | Higher Coding Standards |
Self-contained Languages | Slow Turnaround Time |
Large Debugging Toolbox |
Oct 2020 | Oct 2019 | Programming Language | Rantings | Change |
1 | 2 | C | 16.95% | 0.77% |
2 | 1 | Java | 12.56% | -4.32% |
3 | 3 | Python | 11.28% | +2.19% |
4 | 4 | C++ | 6.94% | +0.71% |
5 | 5 | C# | 4.16% | +0.30% |
6 | 6 | Visual Basic | 3.97% | +0.23% |
8 | 9 | PHP | 2.09% | +0.18% |
9 | 15 | R | 1.99% | +0.73% |
10 | 8 | SQL | 1.57% | -0.37% |
Python is an Object-oriented Programming Language. All Entities (Strings, Integers, Functions etc.) in Python are Objects. An Object is always an instance of a specific Class.
A Class is a template or prototype for creating objects. We can create as many object based on the same class but these would be independent and different from each other. We can perform different operations using different objects. Each such object is called Instance of that specific class.
Each object has Attributes. An Object Attribute is called a Method if its value is a Function. Each object would have: Own Attributes and Inherited Attributes.
Some of the main data types in Python are:
''
or ""
. We can modify the values in dictionaries.print('Hello, world!')
print("Hello, world!")
Hello, world! Hello, world!
print(1)
print(-1)
1 -1
The Boolean values begin with a capital alphabet. If we use lowercase alphabets, then Python will throw an error as Python will interpret them as names of variables.
print(True)
print(False)
True False
# Empty List
print("Empty List: {}".format([]))
Empty List: []
# List with Elements
print("Elements in the List: {}".format([1,2,3,4,5]))
Elements in the List: [1, 2, 3, 4, 5]
# Empty Dictionary
print("Empty List: {}".format({}))
Empty List: {}
# Non-empty Dictionary
print("Empty List: {}".format({'key': 1}))
Empty List: {'key': 1}
We can create a dictionary in multiple lines as well.
# Non-empty Dictionary
dictionary = {
'key_1': 1,
'key_2': 2
}
print("Empty List: {}".format(dictionary))
Empty List: {'key_1': 1, 'key_2': 2}
Use the cells below to make the following calculations:
# Addition
20 + 30
50
# Subtraction
20 - 30
-10
# Multiplication
20 * 30
600
# Division
20 / 30
0.6666666666666666
# Exponent
20 ** 30
1073741824000000000000000000000000000000
# Create a variable `a` and assign the value `20`, so `a = 20`
a = 20
# Create a variable `b` and assign the value `30`
b = 30
# Add `a` and `b`, the outcome should be 50
a + b
50
# Multiply `a` by `b`, the outcome should be 600
a * b
600
# Reassign and print
x = 100
print(x * y)
186360017696000
# Print undeclared variable
# print(c)
# uncomment the above statement to see the output
The output obtained for the above case is:
NameError Traceback (most recent call last)
<ipython-input-1-c0be985ee631> in <cell line: 2>()
1 # Print undeclared variable
----> 2 print(c)
NameError: name 'c' is not defined
Calculate the value of c
in $a^{2}$ + $b^{2}$ = $c^{2}$? (Pythagorean Theorem)
a = 4
b = 3
c = ((a ** 2) + (b ** 2)) ** 0.5
print(c)
5.0
print()
Statement¶Every print()
statement uses it's own line. For example:
x = 4
y = 5
print(x)
print(y)
4 5
We can print the values together as well, using ','
, which will place a space between the values of the variables as:
city = "New Delhi"
country = "India"
print(city, country)
New Delhi India
The above way to print is a default way to print using the separator but we can add some parameters as well, for example:
print(city, country, sep=" : ")
New Delhi : India
By default, we have the \n
as the default character but we can change it by assigning the required value to the using the end
parameter of print()
statement.
print(city, end=".")
print(country)
New Delhi.India
Since we changed the end-of-line character for the first statement, we can see it in the output that both the values are printed in the same line.
We can create our own functions in Python as:
def my_function(a, b):
a = a + 1
c = a + b
return c
The user=defined functions consist of the following:
If we do not mention the return statement, then Python will just return null
as the return value.
print()
is one of the built-in function. We use the parenthesis ()
to call a function. Usually when we call a function, we have to pass an argument.
abs()
returns the absolute value of a number.
Absolute_value = abs(-42)
print(Absolute_value)
42
any()
returns True if at least one element of an iterable is True.
Bool_list = [True, False, True, True]
print(any(Bool_list))
True
all()
returns True if all elements of an iterable are True.
print(all(Bool_list))
False
chr()
converts an ASCII Value to a character.
Char_value = chr(65)
print(Char_value)
A
ord()
returns the Unicode code point of a character.
Unicode_value = ord('€')
print(Unicode_value)
8364
The len()
function returns the number of items in an object. It works with sequences like strings, lists, and tuples.
string_length = len("Hello, Python!")
print(string_length)
14
list_length = len([1,2,3,4,5])
print(list_length)
5
max()
and min()
return the maximum and minimum values of a sequence or multiple arguments.
x = min(5, 10, 25)
print("Minimum value in the list: {}".format(x))
Minimum value in the list: 5
y = max(5, 10, 25)
print(y)
print("Maximum value in the list: {}".format(y))
25 Maximum value in the list: 25
type()
returns the type of an object. It will provide the name of the class which is used for the creation of the specific object. This is particularly useful when dealing with dynamic typing in Python.
Var_type = type(42)
print(Var_type)
<class 'int'>
Var_type_str = type("Hello")
print(Var_type_str)
<class 'str'>
We can take an input from the user using the input()
method. We can take input in the terminal of the application when we execute it. This method will take the value and assign it to the variable.
print('Enter your name:')
x = input()
print('Hello, ' + x)
Enter your name: Aashish Hello, Aashish
We can use the id()
function to obtain the location of a specific object in the memory.
x = ('apple', 'banana', 'cherry')
y = id(x)
print("The Memory Location where 'x' is stored is: {}".format(y))
The Memory Location where 'x' is stored is: 1863600276224
round()
rounds a floating-point number to the nearest integer.
Rounded_value = round(3.14159)
print(Rounded_value)
3
sorted()
returns a sorted list from the specified iterable.
Unsorted_list = [5,1,8,3,2]
Sorted_list = sorted(Unsorted_list)
print(Sorted_list)
[1, 2, 3, 5, 8]
sum()
calculates the sum of all items in an iterable.
Numbers = [1,2 3,4,5]
Sum_result = sum(Numbers)
print(Sum_result)
Cell In[52], line 1 Numbers = [1,2 3,4,5] ^ SyntaxError: invalid syntax. Perhaps you forgot a comma?