Python Introduction
Python Introduction
Python Introduction
• Python is portable which means python can run on different platform with less or
no modifications.
Introduction to Python Programming
Python Interpreter
Compiler
Output:
Enter your name: GFG
Hello, GFG
<class 'str'>
Introduction to Python Programming
Debugging
Programming is error –prone, programming errors are called bugs and process of tracking
them is called debugging.
Three kinds of error can occur in a program: a. Syntax Error b. Runtime Error
c. Semantic error.
Syntax error:
• Syntax refers to the structure of a program and rules about the structure.
• Python can only execute a program if the syntax is correct otherwise the interpreter
display an error message.
Introduction to Python Programming
Runtime Error:
The error that occurs during the program execution is called run time error.
Semantic Error:
The computer will not generate any error message but it will not do the right thing since
the meaning of the program is wrong.
Example:
>>>type(5) # data type : integer
<class 'int'>
>>>type(83.0) ) # data type : float
<class 'float’>
>>>type('Hello, World!’) # data type : string
<class 'str’>
Python Keywords:
• Keywords are reserved words that cannot be used as ordinary identifiers.
• All the keywords except True, False and None are in lowercase
Introduction to Python Programming
Python Identifier
Identifiers are names for entities in a program such as class, variables and functions
etc.
Rules for defining Identifiers:
• Identifiers can be composed of uppercase, lowercase letters, underscore and digits
should start only with an alphabet or an underscore.
• Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z)
or digits or an underscore.
• Identifiers cannot start with digit
• Keywords cannot be used as identifiers.
• Only ( _ ) underscore special symbol can be used.
Introduction to Python Programming
Variables
A variable is nothing but a reserved memory location to store values. A variable in a
program gives data to the computer.
Example
>>>b=20
>>>print(b)
Introduction to Python Programming
Variables
A variable is nothing but a reserved memory location to store values. A variable in a
program gives data to the computer.
Example
>>>b=20
>>>print(b)
Introduction to Python Programming
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
The indentation in Python is very important.
Python uses indentation to indicate a block of code.
Example: Correct Indentation
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Output
Five is greater than two!"
Five is greater than two!"
Introduction to Python Programming
Output
File "demo_indentation2_error.py", line 3
print("Five is greater than two!")
^
IndentationError: unexpected indent
Introduction to Python Programming
Expressions
An Expression is a combination of values, variables and operators
Example
>>>10+20
12
Statements
• A Statement is an instruction that a python interpreter can execute.
Example
c=a+b
• Multiline statement can be used in single line using semicolon(;)
Example
>>a=1;b=10;c=a +b
Introduction to Python Programming
Comments
Comments are non-executable statements which explain what program does.
There are two ways to represent a comment.
Single Line Comment
Begins with # hash symbol
Example
#This is a comment
print("Hello, World!")
Multiline Comments
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Introduction to Python Programming
Types of Operator:
Arithmetic Operator
It perform some arithmetic operations. Consider the values of a=10, b=20 for the
following table.
Introduction to Python Programming
a%b
% Modulo It divides and return the remainder 0
Logical Operator
Logical Operators are used to combine two or more condition and perform logical
operations using Logical AND, Logical OR, Logical Not.
Description
Operator Example
Anyone of the
If(a<b or a!=b)
OR condition should be
true
Standard Datatypes
A datatype is a category for values and each value can be of different types. There are
7 data types mainly used in python interpreter.
a) Integer
b) Float
c) Boolean
d) String
e) List
f) Tuple
g) Dictionary
Introduction to Python Programming
Integer
Let Integer be positive values, negative values and zero.
Example:
>>>2+2
4
>>>a=-20
>>> type(a)
output
<type ‘int’>
Float
A floating point value indicates number with decimal point.
>>> a=20.14
>>>type(a) <type ‘float’>
Introduction to Python Programming