0% found this document useful (0 votes)
61 views56 pages

Python Introduction-1

The document provides an introduction to the Python programming language. It defines what a programming language is and explains that Python is a high-level, interpreted, and general-purpose programming language. It describes key features of Python like being high-level, free and open source, portable, having a rich library of functions, and using indentation. The document also covers Python installation, basic program structure, keywords, identifiers, variables, operators, data types including numeric, string, tuple and list, and immutable and mutable data types.

Uploaded by

Jay PRAJAPATI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
61 views56 pages

Python Introduction-1

The document provides an introduction to the Python programming language. It defines what a programming language is and explains that Python is a high-level, interpreted, and general-purpose programming language. It describes key features of Python like being high-level, free and open source, portable, having a rich library of functions, and using indentation. The document also covers Python installation, basic program structure, keywords, identifiers, variables, operators, data types including numeric, string, tuple and list, and immutable and mutable data types.

Uploaded by

Jay PRAJAPATI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 56

Unit-2-Python Introduction

What is a programming language?


● An ordered set of instructions to be executed by a computer to carry out a specific task is
called a program, and the language used to specify this set of instructions to the computer
is called a programming language.

● 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.

● It was created by Guido van Rossum during 1985- 1990.

● It uses English keywords frequently where as other languages use


punctuation, and it has fewer syntactical constructions than other languages.
Python Features
● Python is a high level language. It is a free and open source language.

● It is an interpreted language, as Python programs are executed by an


interpreter.

● Python programs are easy to understand as they have a clearly defined


syntax and relatively simple structure.

● 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 has a rich library of predefined functions.

● Python is also helpful in web development. Many popular web services and
applications are built using Python.

● Python uses indentation for blocks and nested blocks.


Applications of Python Programming
Python Installation
Step 1: Select Version of Python to Install
Step 2: Download Python Executable Installer
https://www.python.org/downloads/
Step 3: Run Executable Installer
Python Installation
Step 4: Verify Python Was Installed On Windows

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:

Save file with an extension of .py


Cont..
● Comments:
#This is comment statement.
""" This example will demonstrate
multiple comments """
● Symbols used for writing comments include Hash (#) or Triple Double
Quotation marks(""").
● Hash is used in writing single line comments that do not span multiple lines.
● Triple Quotation Marks are used to write multiple line comments.
● Three triple quotation marks to start the comment and again three quotation
marks to end the comment.
Cont..
Line Indentation:

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:

print(“a is greater than b”)

else

print(“b is greater than a”)


Keywords
● Keywords are the reserved words in Python.
● We cannot use a keyword as a variable name, function name or any other
identifier.
● In Python, keywords are case sensitive.
● There are 33 keywords in Python 3.7. This number can vary slightly over the
course of time.
● All the keywords except True, False and None are in lowercase and they must
be written as they are. The list of all the keywords is given below.
Cont..
Identifiers
● An identifier is a name given to entities like class, functions, variables, etc. It helps
to differentiate one entity from another.
Rules for writing identifiers

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z)


or digits (0 to 9) or an underscore _. Names like myClass, var_1 and
print_this_to_screen, all are valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid
name.
3. Keywords cannot be used as identifiers.
Cont..
● We cannot use special symbols like !, @, #, $, % etc. in our identifier.
● An identifier can be of any length.
Variables
● Variables are containers for storing data values.
● Python has no command for declaring a variable.
● A variable is created the moment you first assign a value to it.
● x=5
● y = "John"
● print(x)
● print(y)
● Variable names are case-sensitive.
● a=4
● A = "Sally"
● #A will not overwrite a
Operators
● Operators are used to perform operations on variables and values.
● In the example below, we use the + operator to add together two values:

print(10 + 5)

● Python divides the operators in the following groups:


○ Arithmetic operators
○ Assignment operators
○ Comparison operators
○ Logical operators
○ Identity operators
○ Membership operators
○ Bitwise operators
Python Arithmetic Operators

● Arithmetic operators are used with numeric values to perform common


mathematical operations:
Python Assignment Operators

Assignment operators are used to assign values to variables:


Cont..
Python Comparison Operators

Comparison operators are used to compare two values:


Python Logical Operators

Logical operators are used to combine conditional statements:


Python Identity Operators

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

Membership operators are used to test if a sequence is presented in an object:


Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:


Python Data Types

● Data types are the classification or categorization of data items.


● It represents the kind of value that tells what operations can be performed on
a particular data.
● Since everything is an object in Python programming, data types are actually
classes and variables are instance (object) of these classes.
● Following are the standard or built-in data type of Python:
○ Numeric

○ 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

print("Type of a: ", type(a))

b = 5.0

print("\nType of b: ", type(b))

c = 2 + 4j

print("\nType of c: ", type(c))


Output
Type of a: <class 'int'>

Type of b: <class 'float'>

Type of c: <class 'complex'>


● type() function is used to determine the type of data type.
Strings
● A string is a collection of one or more characters put in a single quote,
double-quote or triple quote.
● In python there is no character data type, a character is a string of length one.
It is represented by str class.
Program
s1=”Hello”

print(s1)

print(type(s1))

Output:

Hello

<class 'str'>
Program: Output:
x='''Hello'''
Hello
print(x)

print(type(x)) <class 'str'>

Hello
k12='Hello'

print(k12) <class 'str'>


print(type(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 { }.

A set is similar to list, except that it cannot have duplicate entries.


Example

#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.

1. Implicit Type Conversion


2. Explicit Type Conversion
Implicit Type Conversion
● In Implicit type conversion, Python automatically converts one data type to
another data type. This process doesn't need any user involvement.
Example 1: Converting integer to float
num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

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:

datatype of num_int: <class 'int'>

datatype of num_flo: <class 'float'>

Value of num_new: 124.23

datatype of num_new: <class 'float'>


Example 2: Addition of string(higher) data type and
integer(lower) datatype
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str:",type(num_str))

print(num_int+num_str)
Cont..
Output:

Data type of num_int: <class 'int'>

Data type of num_str: <class 'str'>

Traceback (most recent call last):

File "python", line 7, in <module>

TypeError: unsupported operand type(s) for +: 'int' and 'str'


Explicit Type Conversion
● In Explicit Type Conversion, users convert the data type of an object to
required data type. We use the predefined 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 3:Addition of string and integer using explicit
conversion
num_int = 123
num_str = "456"
print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
Cont..
Output:
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579


Data type of the sum: <class 'int'>
Explicit Type Conversion
● x = str(3) # x will be '3'
● y = int(3) # y will be 3
● z = float(3) # z will be 3.0
Thank You

You might also like