In today's session:
Getting Started with Python
• Introduction to Python
• Python Keywords
• Identifiers
• Comments
• Data Types
• Operators
• Expressions
• Statement
• Input and Output
• Type Conversion
• Debugging
• Practice!
UNIT RDBMS:
Data
manupulation
using SQL
@aakash7.7
Introduction to python
• A program written in a high-level language is called source code.
• An interpreter processes the program statements one by one,
first translating and then executing.
Introduction to python
• A program written in a high-level language is called source code.
• An interpreter processes the program statements one by one,
first translating and then executing.
• Python uses an interpreter to convert its instructions into
machine language, so that it can be understood by the computer.
• This process is continued until an error is encountered or the
whole program is executed successfully
Features of Python
• 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.
Features of Python
• 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.
• Python uses indentation for blocks and nested blocks
Working with Python
• To write and run (execute) a Python program, we need to have a
Python interpreter installed on our computer or we can use any
online Python interpreter.
Script Mode:
• In this mode source code is stored in a file with the .py extension
and use the interpreter to execute the contents of the file.
• To execute the script by the interpreter, you have to tell the
interpreter the name of the file.
Python keywords
• Reserved words in the library of a language. There are
33 keywords in python.
• All the keywords are in lowercase except 03 keywords
(True, False, None)
Identifiers
• The name given by the user to the entities like
variable name etc.
Variables
• Variable in Python refers to an object — an item
or element that is stored in the memory.
• A variable in a program is uniquely identified by a name
(identifier).
Comments
• Comments are not executed.
• Comments explain a program and make a program
understandable and readable.
• Comments start with #
Data Types
• List is a sequence of items : [5, "Sanjana", "11C", 45]
• Tuple is a sequence of items : (10, 20, "Apple", 3.4, 'a')
• Dictionary holds data items in key-value pairs {'Fruit' : 'Apple', 'Price' : 120}
Data Types
Operators : Arithmetic
Operators: Relational
Operators: Assignment
Operators: Logical
Operators: Identity
Operators: Membership
Expressions
• An expression is defined as a combination of constants,
variables, and operators.
• An expression always evaluates to a value
• (20 + 30) * 40
• 3.0 + 3.14
• "Global" + "Citizen"
Statement
• In Python, a statement is a unit of code that the Python
interpreter can execute
• x=4
• cube = x ** 3
• print (x, cube)
Input and Output
• The input() function prompts the user to enter data. It accepts all
user input as string.
• fname = input("Enter your first name: ")
• Enter your first name: Aakash
• age = input("Enter your age: ")
• Enter your age: 19
• print()
Type Conversion
• Consider the following program
num1 = input("Enter a number and I'll double it: ")
num1 = num1 * 2
print(num1)
• The program was expected to display double the value of the
number received and store in variable num1.
Type Conversion : Explicit
Type Conversion : Explicit
Type Conversion : Implicit
Debugging
• A programmer can make mistakes while writing a program, and
hence, the program may not execute or may generate wrong
output.
• The process of identifying and removing such mistakes, also
known as bugs or errors, from a program is called debugging.
Debugging:
Syntax Errors:
• The interpreter interprets the statements only if it is syntactically
(as per the rules of Python) correct.
• X = (10 + 12)
• Y = (7 + 11
Debugging:
Logical Errors:
• Logical errors are also called semantic errors as they occur when
the meaning of the program (its semantics) is not correct
• Average of two numbers: (a+b)/2
• Logical Error: a+b/2
Debugging:
Runtime Errors:
• A runtime error causes abnormal termination of program while
it is executing.
• Runtime errors do not appear until after the program starts
running or executing.
A = 10
B=0
Print(A/B)
Practice:
Practice:
1. What is the difference between a keyword and an identifier?
• Keyword is a pre-defined word that has a special meaning and
purpose. Keywords are reserved and are few. For example : if,
else, elif etc.
• Identifier is the user-defined name given to a part of a program
like variable, object, functions etc.
• Identifiers are not reserved. These are defined by the user but
they can have letters, digits and a symbols underscore.
Practice:
2. Which of the following identifier names are invalid and why?
Practice:
3. Write logical expressions corresponding to the following
statements in Python and evaluate the expressions.
• The sum of 20 and –10 is less than 12.
• num3 is not more than 24
• 6.75 is between the values of integers num1 and num2.
• The string ‘middle’ is larger than the string ‘first’ and smaller
than the string ‘last’
• List Stationery is empty
Practice:
4. Add a pair of parentheses to each expression so that it
evaluates to True.
• 0 == 1 == 2
• 2 + 3 == 4 + 5 == 7
• 1 < -1 == 3 > 4
Practice:
5. Write the output of the following
num1 = 4
num2 = num1 + 1
num1 = 2
print (num1, num2)
num1, num2 = 2, 6
num1, num2 = num2, num1 + 2
print (num1, num2)
num1, num2 = 2, 3
num3, num2 = num1, num3 + 1
print (num1, num2, num3)
Practice:
6. Give the output of the following when num1 = 4, num2 = 3,
num3 = 2
num1 = num1 ** (num2 + num3)
print (num1)
num1 **= num2 + num3
num1 = '5' + '5'
print(num1)
num1 = float(10)
print (num1)
print('Bye' == 'BYE')
print(10 != 9 and 20 >= 20)
Practice:
7. Categorize the following as syntax error, logical error or runtime
error:
25 / 0
num1 = 25; num2 = 0; num1 / num2
Thank you!