Python Introduction-1
Python Introduction-1
● As we know that computers understand the language of 0s and 1s which is called machine
language or low level language.
● However, it is difficult for humans to write or comprehend instructions using 0s and 1s. This
led to the advent of high-level programming languages like Python, C++, Visual Basic, PHP,
Java that are easier to manage by humans but are not directly understood by the computer.
Cont..
● A program written in a high-level language is called source code.
● The language translators like compilers and interpreters are needed to translate the
source code into machine language.
● Python uses an interpreter to convert its instructions into machine language, so that
it can be understood by the computer.
● An interpreter processes the program statements one by one, first translating and
then executing. This process is continued until an error is encountered or the whole
program is executed successfully. In both the cases, program execution will stop.
Cont..
● On the contrary, a compiler translates the entire source code, as a whole, into
the object code. After scanning the whole program, it generates error
messages, if any.
Introduction to Python
● Python is a general-purpose interpreted, interactive, object-oriented, and
high-level programming language.
● Python is case-sensitive. For example, NUMBER and number are not same in
Python.
Cont..
● Python is portable and platform independent, means it can run on various
operating systems and hardware platforms.
● Python is also helpful in web development. Many popular web services and
applications are built using Python.
1. Navigate to the directory in which Python was installed on the system. In our
case, it is C:\Users\Username\AppData\Local\Programs\Python\Python37
since we have installed the latest version.
2. Double-click python.exe.
3. The output should be similar to what you can see below:
Basic Structure of Python Program
Basic Python printing script:
print('Hello, World')
File Extension:
Indentation in Python refers to the (spaces and tabs) that are used at the beginning of a
statement.
The statements with the same indentation belong to the same group called a suite.
If a>b:
else
print(10 + 5)
Identity operators are used to compare the objects, not if they are equal, but if they
are actually the same object, with the same memory location:
Python Membership Operators
○ Sequence Type
○ Boolean
○ Set
○ Dictionary
Python Data Types
Numeric
● In Python, numeric data type represent the data which has numeric value.
● Numeric value can be integer, floating number or even complex numbers.
● These values are defined as int, float and complex class in Python.
● Integers – This value is represented by int class. It contains positive or
negative whole numbers (without fraction or decimal). In Python there is no
limit to how long an integer value can be.
● Float – This value is represented by float class. It is a real number with
floating point representation. It is specified by a decimal point.
● Complex Numbers – Complex number is represented by complex class. It is
specified as (real part) + (imaginary part)j. For example – 2+3j
Program
# Python program to demonstrate numeric value
a=5
b = 5.0
c = 2 + 4j
print(s1)
print(type(s1))
Output:
Hello
<class 'str'>
Program: Output:
x='''Hello'''
Hello
print(x)
Hello
k12='Hello'
<class 'str'>
string="Hello"
print(string)
print(type(string))
Immutable and Mutable Data Types
● An immutable data types/object can't be changed after it is created.
● These are of in-built types like int, float, bool, string, unicode, tuple.
● Custom classes are generally mutable. These are of type list, dict, set .
● A mutable data types/object can be changed after it is created.
Immutable and Mutable Data Types
Tuple
Tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ).
This is unlike list,where values are enclosed in brackets [ ].
Once created, we cannot change the tuple.
Example
#create a tuple tuple1
>>> tuple1 = (10, 20, "Apple", 3.4, 'a')
#print the elements of the tuple tuple1
>>> print(tuple1)
(10, 20, "Apple", 3.4, 'a')
List
List is a sequence of items separated by commas and
the items are enclosed in square brackets [ ].
Example
#To create a list
>>> list1 = [5, 3.4, "New Delhi", "20C", 45]
#print the elements of the list list1
>>> print(list1)
[5, 3.4, 'New Delhi', '20C', 45]
Set
Set is an unordered collection of items separated by commas and the items are enclosed in curly
brackets { }.
#create a set
>>> set1 = {10,20,3.14,"New Delhi"}
>>> print(type(set1))
<class 'set'>
>>> print(set1)
{10, 20, 3.14, "New Delhi"}
#duplicate elements are not included in set
>>> set2 = {1,2,1,3}
>>> print(set2)
{1, 2, 3}
Type Conversion
● If you want to specify the data type of a variable, this can be done with
casting.
● There may be times when you want to specify a type on to a variable. This
can be done with casting.
● Casting in python is therefore done using constructor functions:
● int() - constructs an integer number from an integer literal, a float literal (by
removing all decimals), or a string literal (providing the string represents a
whole number)
● float() - constructs a float number from an integer literal, a float literal or a
string literal (providing the string represents a float or an integer)
● str() - constructs a string from a wide variety of data types, including strings,
integer literals and float literals
Type Conversion
The process of converting the value of one data type (integer, string, float, etc.) to another data
type is called type conversion. Python has two types of type conversion.
print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))
print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
Cont..
Output:
print(num_int+num_str)
Cont..
Output: