Python 1
Python 1
Python History
• Python laid its foundation in the late 1980s.
• The implementation of Python was started in December 1989 by Guido Van Rossum at CWI in
Netherland.
• In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to alt.sources.
• In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce.
• Python 2.0 added new features such as list comprehensions, garbage collection systems.
• On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify the
fundamental flaw of the language.
• ABC programming language is said to be the predecessor of Python language, which was capable of
Exception Handling and interfacing with the Amoeba Operating System.
• The following programming languages influence Python:
• ABC language.
• Modula-3
Why the Name Python?
• There is a fact behind choosing the name Python. Guido van
Rossum was reading the script of a popular BBC comedy series
"Monty Python's Flying Circus". It was late on-air 1970s.
• Van Rossum wanted to select a name which unique, sort, and little-bit
mysterious. So he decided to select naming Python after the "Monty
Python's Flying Circus" for their newly created programming
language.
• The comedy series was creative and well random. It talks about
everything. Thus it is slow and unpredictable, which made it very
interesting.
Python Applications
• Python is known for its general-purpose nature that makes it
applicable in almost every domain of software development. Python
makes its presence in every emerging field. It is the fastest-growing
programming language and can develop any application.
Here, we are specifying application areas where Python can be
applied.
Comments in Python
• In Python, we can apply comments using the # hash character. The
Python interpreter entirely ignores the lines followed by a hash
character. A good programmer always uses the comments to make
code under stable. Let's see the following example of a comment.
• name = “Deepak" # Assigning string value to the name variable
Python Variables
• Variable is a name that is used to refer to memory location. Python
variable is also known as an identifier and used to hold value.
• In Python, we don't need to specify the type of variable because
Python is a infer language and smart enough to get variable type.
• Variable names can be a group of both the letters and digits, but they
have to begin with a letter or an underscore.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals
used in the program. The rules to name an identifier are given below.
• The first character of the variable must be an alphabet or underscore ( _ ).
• All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore, or digit (0-9).
• Identifier name must not contain any white-space, or special character (!, @, #, %,
^, &, *).
• Identifier name must not be similar to any keyword defined in the language.
• Identifier names are case sensitive; for example, my name, and MyName is not the
same.
• Examples of valid identifiers: a123, _n, n_9, etc.
• Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
• Python does not bind us to declare a variable before using it in the
application. It allows us to create a variable at the required time.
• We don't need to declare explicitly variable in Python. When we
assign any value to the variable, that variable is declared
automatically.
• The equal (=) operator is used to assign value to a variable.
Object References
• It is necessary to understand how the Python interpreter works when
we declare a variable. The process of treating variables is somewhat
different from many other programming languages.
• Python is the highly object-oriented programming language; that's
why every data item belongs to a specific type of class. Consider the
following example.
• print("John")
• type("John")
Object Identity
a = 50
b = a
print(id(a))
print(id(b))
# Reassigned variable a
a = 500
print(id(a))
Consider the following valid variables name.
name = "A"
Name = "B"
naMe = "C"
NAME = "D"
n_a_m_e = "E"
_name = "F"
name_ = "G"
_name_ = "H"
na56me = "I"
print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, na56me)
Multiple Assignment
x=y=z=50
print(x)
print(y)
print(z)
Assigning multiple values to multiple
variables:
a,b,c=5,10,15
print a
print b
print c
Python Variable Types
• Local Variable
Local variables are the variables that declared inside the function and have scope within
the function. Let's understand the following example.
# Declaring a function
def add():
# Defining local variables. They has scope only within a function
a = 20
b = 30
c = a + b
print("The sum is:", c)
# Calling a function
add()
Global Variables
• A variable declared outside the function is the global variable by default. Python provides
the global keyword to use global variable inside the function. If we don't use the global keyword,
the function treats it as a local variable. Let's understand the following example.
# Declare a variable and initialize it
x = 101
# Global variable in function
def mainFunction():
# printing a global variable
global x
print(x)
# modifying a global variable
x = 'Welcome To Javatpoint'
print(x)
mainFunction()
print(x)
Python Data Types
Numbers
a = 5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
• Int: Whole number worth can be any length, like numbers 10, 2, 29, -
20, - 150, and so on. An integer can be any length you want in Python.
Its worth has a place with int.
• Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc. It
can be accurate to within 15 decimal places.
• Complex: An intricate number contains an arranged pair, i.e., x + iy,
where x and y signify the genuine and non-existent parts separately.
The complex numbers like 2.14j, 2.0 + 2.3j, etc.
Sequence Type
• String
The sequence of characters in the quotation marks can be used to describe the string. A
string can be defined in Python using single, double, or triple quotes.
String dealing with Python is a direct undertaking since Python gives worked-in
capabilities and administrators to perform tasks in the string.
e.g.
str = "string using double quotes"
print(str)
s = '''''A multiline
string'''
print(s)
str1 = 'hello javatpoint' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
List
• Lists in Python are like arrays in C, but lists can contain data of
different types. The things put away in the rundown are isolated with a
comma (,) and encased inside square sections [].
• To gain access to the list's data, we can use slice [:] operators. Like
how they worked with strings, the list is handled by the concatenation
operator (+) and the repetition operator (*).
• Look at the following example.
list1 = [1, "hi", "Python", 2]
#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
print (list1 * 3)
Tuple
• In many ways, a tuple is like a list. Tuples, like lists, also contain a
collection of items from various data types. A parenthetical space ()
separates the tuple's components from one another.
• Because we cannot alter the size or value of the items in a tuple, it is a
read-only data structure.
• Let's look at a straightforward tuple in action.
tup = ("hi", "Python", 2)
# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
# Adding value to tup. It will throw an error.
t[2] = "hi"
Dictionary
• A dictionary is a key-value pair set arranged in any order. It stores a
specific value for each key, like an associative array or a hash table.
Value is any Python object, while the key can hold any primitive data
type.
• The comma (,) and the curly braces are used to separate the items in
the dictionary.
• Look at the following example.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d.keys())
print (d.values())
Set
• The data type's unordered collection is Python Set. It is iterable,
mutable(can change after creation), and has remarkable components.
The elements of a set have no set order; It might return the element's
altered sequence. Either a sequence of elements is passed through the
curly braces and separated by a comma to create the set or the built-in
function set() is used to create the set. It can contain different kinds of
values.
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)
Python Keywords
False await else import pass
Or
help("keywords")
Value Keywords: True, False, None
print( 4 == 4 ) print( True == 3 )
print( 6 > 9 ) print( False == 0 )
print( True or False ) print( True + True + True)
print( 9 <= 28 )
print( 6 > 9 )
print( True and False )
The None Keyword
print( None == 0 )
print( None == " " )
print( None == False )
A = None
B = None
print( A == B )
Operator Keywords: and, or, not, in, is
Mathematical Operations Operations in Other Languages Python Keyword
OR, ∨ || or
NOT, ¬ ! not
CONTAINS, ∈ in
IDENTITY === is
Truth table for and Truth table for or
X Y X and Y X Y X or Y
X not X
True False
False True
The in Keyword
• <an_element> in <a_container>
container = "Javatpoint"
print( "p" in container )
print( "P" in container )
The is Keyword
print( True is True )
print( False is True )
print( None is not None )
print( (9 + 5) is (7 * 2) )