Python Day 3
Python Day 3
Introduction to Python
What is Python?
Python is a very popular general-purpose interpreted, interactive, object-
oriented, and high-level programming language. Python is dynamically-typed and
garbage-collected programming language. It was created by Guido van Rossum
during 1985- 1990. Like Perl, Python source code is also available under the GNU
General Public License (GPL).
Python supports multiple programming paradigms, including Procedural,
Object Oriented and Functional programming language. Python design philosophy
emphasizes code readability with the use of significant indentation.
Python Jobs
Today, Python is very high in demand and all the major companies are
looking for great Python Programmers to develop websites, software components,
and applications or to work with Data Science, AI, and ML technologies. When we
are developing this tutorial in 2023, there is a high shortage of Python
Programmers whereas market demands more number of Python Programmers
due to its application in Machine Learning, Artificial Intelligence etc. Today a
Python Programmer with 3-5 years of experience is asking for around $150,000
annual package and this is the most demanding programming language in
America. Though it can vary depending on the location of the Job. It's impossible
to list all of the companies using Python, to name a few big companies are:
• Google
• Intel
• NASA
• PayPal
• Facebook
• IBM
• Amazon
• Netflix
• Pinterest
• Uber
• Many more...
So, you could be the next potential employee for any of these major companies.
We have developed a great learning material for you to learn Python
Programming which will help you prepare for the technical interviews and
certification exams based on Python. So, start learning Python using this simple
and effective tutorial from anywhere and anytime absolutely at your space
Careers in Python
If you know Python nicely, then you have a great career ahead. Here are
just a few of the career options where Python is a key skill:
• Web designer
• Python developer
• Full-stack developer
• Machine learning engineer
• Data scientist
• Data analyst
• Data engineer
• DevOps engineer
• Software engineer
• Game developer
• Many more other roles
Characteristics of Python
Following are important characteristics of Python Programming –
• It supports functional and structured programming methods
as well as OOP.
• It can be used as a scripting language or can be compiled to
byte-code for building large applications.
• It provides very high-level dynamic data types and supports
dynamic type checking.
• It supports automatic garbage collection.
• It can be easily integrated with C, C++, COM, ActiveX,
CORBA, and Java.
Applications of Python
The latest release of Python is 3.x. As mentioned before, Python is
one of the most widely used language over the web. I'm going to list
few of them here:
• Easy-to-learn − Python has few keywords, simple structure,
and a clearly defined syntax. This allows the student to pick
up the language quickly.
• Easy-to-read − Python code is more clearly defined and
visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-
maintain.
• A broad standard library − Python's bulk of the library is
very portable and crossplatform compatible on UNIX,
Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive
mode which allows interactive testing and debugging of
snippets of code. Python Tutorial iv
• Portable − Python can run on a wide variety of hardware
platforms and has the same interface on all platforms.
• Extendable − You can add low-level modules to the Python
interpreter. These modules enable programmers to add to or
customize their tools to be more efficient.
• Databases − Python provides interfaces to all major
commercial databases.
• GUI Programming − Python supports GUI applications that
can be created and ported to many system calls, libraries
and windows systems, such as Windows MFC, Macintosh,
and the X Window system of Unix.
• Scalable − Python provides a better structure and support
for large programs than shell scripting.
Variables are like containers for storing values. Values in the variables can be
changed.
Values
Consider that variables are like containers for storing information. In context of
programming, this information is often referred to as value.
Data Type
All whole numbers (positive, negative and zero) without any fractional part come
under Integers. Examples
...-3, -2, -1, 0, 1, 2, 3,...
Float
In a general sense, anything that can take one of two possible values is considered
a Boolean. Examples include the data that can take values like
• True or False
• Yes or No
• 0 or 1
• On or Off, etc.
As per the Python Syntax,
True and False
are considered as Boolean values. Notice that both start with a capital letter.
Assigning Value to Variable
The following is the syntax for assigning an integer value 10 to a variable age
age = 10
Here the equals to = sign is called as Assignment Operator as it is used to assign
values to variables.
Expression
String Concatenation
Joining strings together is called string concatenation.
Eg:
a = "Hello" + " " + "World"
print(a)
output: Hello World
String Repetition
Length of String
len()
returns the number of characters in a given string.
Eg:
username = input()
length = len(username)
print(length)
Input
Ravi
Output
4
String Slicing
If end index is not specified, slicing stops at the end of the string.
message = "Hi Ravi"
part = message[3:]
print(part)
Output
Ravi
print(type(10))
print(type(4.2))
print(type("Hi"))
Output
<class 'int'>
<class 'float'>
<class 'str'>
Type Conversion
Converting the value of one data type to another data type is called Type
Conversion or Type Casting. We can convert
• String to Integer
• Integer to Float
• Float to String and so on.
String to Integer int() converts valid data of any type to integer
a = "5"
a = int(a)
print(type(a))
print(a)
Output
<class 'int'>
Summary
Relational Operators
Operator Name
== Is equal to
!= Is not equal to
print(5 < 10)
print(2 > 1)
Output
True
True
Comparing Numbers
print(2 <= 3)
print(2.53 >= 2.55)
Output
True
False
print(12 == 12.0)
print(12 == 12.1)
Output
True
False
Comparing Strings
print("ABC" == "ABC")
print("CBA" != "ABC")
Output
True
True
Case Sensitive
print("ABC" == "abc")
Output
False
Python is case sensitive. It means X (Capital letter) and x (small letter) are not the
same in Python.
print(True == "True")
print(123 == "123")
print(1.1 == "1.1")
Output
False
False
False
Logical Operators
A B A or B
A Not A
True False
False True
Conditional Statements
Block of Code
Conditional Statement allows you to execute a block of code only when a specific
condition is True
Conditional Block
Block of code which executes only if a condition is True is called Conditional Block.
Indentation
• Space(s) in front of the conditional block is called indentation.
• Indentation(spacing) is used to identify Conditional Block.
• Standard practice is to use four spaces for indentation.
If - Else Syntax
When If - Else conditional statement is used, Else block of code executes if the
condition is False
Using If-Else
a = int(input())
if a > 0:
print("Positive")
else:
print("Not Positive")
print("End")
Input
2
Output
Positive
End
Loops
So far we have seen that Python executes code in a sequence and each block of
code is executed once.
Loops allow us to execute a block of code several times.
While Loop
Allows us to execute a block of code several times as long as the condition is True
.
The following code snippet prints the next three consecutive numbers after a
given number.
a = int(input())
counter = 0
while counter < 3:
a=a+1
print(a)
counter = counter + 1
Input
4
Output
5
6
7
For Loop
Examples of sequences:
• Sequence of Characters (string)
• Sequence of numbers, etc.
Eg:
word = "Python"
for each_char in word:
print(each_char)
Output
P
y
t
h
o
n
Range