Python Btech
Python Btech
Introduction to Python
History of Python Programming Language
🧑💻 Creator:
Guido van Rossum, a Dutch programmer, started working on Python in the late 1980s.
He officially released it as Python 0.9.0 in February 1991.
Guido named it "Python" after the British comedy group Monty Python, not the snake.
He wanted a name that was short, unique, and slightly mysterious.
Included classes, functions, exception handling, and core data types like str, list, dict.
Introduced list comprehensions, garbage collection using reference counting and cycle-
detecting GC.
Python 2 series lasted till Python 2.7 (2010), which was supported until January 1, 2020.
🔄 Python Today
The latest stable version is part of the Python 3 series.
Python is one of the most popular programming languages due to its simplicity,
versatility, and wide range of applications: web development, AI/ML, data science,
automation, etc.
🧠 Fun Fact:
Python is open-source and has a huge community contributing to its development via PEPs
(Python Enhancement Proposals).
Python is one of the top programming languages in the world, widely used in
fields such as AI, machine learning, data science, and web development.
The simple and English-like syntax of Python makes it a go-to language for
beginners who want to get into coding quickly.
Because Python is used in multiple fields, there is a high demand for Python
developers, with competitive base salaries.
You can run Python on your computer using the following two methods:
For those who prefer to install Python on your computer, this guide will walk
you through the installation process on Windows, macOS, and Linux
(Ubuntu).
Windows
1. Install VS Code
4. Install Python
Go to the VS Code Official website and download the Windows installer. Once
the download is complete, run the installer and follow the installation
process.
The website automatically detects your operating system and gives you the
right installer.
Now, go to your download folder and run the installer you just downloaded.
Depending on your security settings, you might be prompted to allow access.
Once you have run the installer, you will come across this screen.
On the screen, you will see two options: Install Now and Customize
Installation. We suggest you skip all customization steps and simply
click Install Now.
Click Install Now, as it will include all the necessary files needed later.
This makes it easier to run a Python Program from the command prompt
(cmd) directly without specifying the full path of the Python executable.
After using this option, Python will be successfully installed in your device.
Step 4: Verify your installation
After the installation is complete, you can verify whether Python is installed
by using the following command in the command prompt.
python --version
Note: The version number might differ from the one above, depending on
your installed version.
Now, you are all set to run Python programs on your device.
Run Your First Python Program
First open VS Code, click on the File in the top menu and then select New
File.
Then, save this file with a .py extension by clicking on File again, then Save
As, and type your filename ending in .py. (Here, we are saving it as
Hello_World.py)
Before you start coding, make sure the Python extension is installed in VS
Code. Open VS Code and click on Extensions on the left sidebar. Then, search
for the Python extension by Microsoft and click on install.
Now, write the following code into your file:
print("Hello World")
Then click on the run button on the top right side of your screen.
Now that you have set everything up to run Python programs on your
computer, you'll be learning how the basic program works in Python in the
next tutorial.
In the previous tutorial, you learned how to install Python on your computer.
Now, let's write a simple Python program.
print("Hello, World!")
Output
Hello World!
Congratulations on writing your first Python program. Now, let's see how the
above program works.
The text we want to print is placed within double quotes " ".
We can also use single quotes to print text on the screen. For example,
print('Hello World!')
is same as
print("Hello World!")
Python Comments
In the previous tutorial, you learned to write your first Python program. Now,
let's learn about Python comments.
Important!: We are introducing comments early in this tutorial series
because we will be using them to explain the code in upcoming tutorials.
Comments are hints that we add to our code to make it easier to understand.
Python comments start with #. For example,
# print a number
print(25)
Single-line Comment
We use the hash (#) symbol to write a single-line comment. For example,
# declare a variable
name = "John"
# print name
print(name) # John
# declare a variable
# print name
# John
A single-line comment starts with # and extends up to the end of the line.
We can also use single-line comments alongside the code:
print(name) # John
Unlike languages such as C++ and Java, Python doesn't have a dedicated
method to write multi-line comments.
However, we can achieve the same effect by using the hash (#) symbol at
the beginning of each line.
print("Hello, World!")
'''This is an example
of multiline comment'''
print("Hello, World!")
Output
Hello World
number1 = 10
number2 = 15
For example,
number1 = 10
number2 = 15
Output
The sum is 25
We have resolved the error using a comment. Now if you need to calculate
the product in the near future, you can uncomment it.
Note: This approach comes in handy while working with large files. Instead
of deleting any line, we can use comments and identify which one is causing
an error.
For debugging.
Note: Comments are not and should not be used as a substitute to explain
poorly written code. Always try to write clean, understandable code, and
then use comments as an addition.
In most cases, always use comments to explain 'why' rather than 'how' and
you are good to go.
Python Variables and Literals
In the previous tutorial you learned about Python comments. Now, let's learn
about variables and literals in Python.
Python Variables
number = 10
site_name = 'Facebook'
print(site_name)
# Output: Facebook
Output
site_name = 'Hospital'
print(site_name)
site_name = 'apple.com'
print(site_name)
Output
Hospital
apple.com
a, b, c = 5, 3.2, 'Hello'
Here, we have assigned the same string value 'Asif' to both the
variables site1 and site2.
snake_case
MACRO_CASE
camelCase
CapWords
2. Create a name that makes sense. For example, vowel makes more sense
than v.
3. If you want to create a variable name having two words, use underscore to
separate them. For example:
my_name
current_salary
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
6. Avoid using keywords like if, True, class, etc. as variable names.
Python Literals
There are different types of literals in Python. Let's discuss some of the
commonly used types in detail.
1. Integer Literals
2. Floating-Point Literals
3. Complex Literals
Here, numerals are in the form a + bj, where a is real and b is imaginary. For
example, 6+9j, 2+3j.
In Python, texts wrapped inside quotation marks are called string literals..
"This is a string."
float_number = 1.23
new_number = integer_number + float_number
print("Value:",new_number)
print("Data Type:",type(new_number))
Output
Value: 124.23
Then we added these two variables and stored the result in new_number.
As we can see new_number has value 124.23 and is of the float data type.
It is because Python always converts smaller data types to larger data types
to avoid the loss of data.
Note:
We get TypeError, if we try to add str and int. For example, '12' + 23.
Python is not able to use Implicit Conversion in such conditions.
We use the built-in functions like int(), float(), str(), etc to perform explicit type
conversion.
This type of conversion is also called typecasting because the user casts
(changes) the data type of the objects.
Example 2: Addition of string and integer Using Explicit Conversion
num_string = '12'
num_integer = 23
num_string = int(num_string)
print("Sum:",num_sum)
Output
Sum: 35
num_string = int(num_string)
Finally, we got the num_sum value i.e 35 and data type to be int.
4. Explicit Type Conversion is also called Type Casting, the data types of
objects are converted using predefined functions by the user.
Python Output
In Python, we can simply use the print() function to print output. For
example,
print('Python is powerful')