0% found this document useful (0 votes)
9 views

Introduction

Uploaded by

Sreedhar Yellam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Introduction

Uploaded by

Sreedhar Yellam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

1.

Introduction
(Identifiers,Keywords,Data Types, Type Conversion)
2. Operators
3.Input/ Output Systems
4.Control Statements
5.Pattern Programs
6.Strings
7.Lists
8.Tuples
9.Sets
10.Dictionary
11.Functions
12. Modules ,Packages
1.Introduction
IDENTIFIERS
• An identifier is a name given to entities like
class, functions, variables, etc.
• It helps to differentiate one entity from
another.
IDENTIFIERS
Rules for writing identifiers
• Alphabet Symbols lowercase (a to z) or uppercase (A to Z) or digits (0 to
9) or an underscore _.

Examples : Var_1, name, print_var

• An identifier cannot start with a digit.


Example : 1variable is invalid

• Keywords cannot be used as identifiers.

• An identifier can be of any length.

Python is a Case Sensitive Language


Keywords

• Keywords are the reserved words in Python.


• We cannot use a keyword as a variable name,
function name or any other identifier.
• They are used to define the syntax and
structure of the Python language.
Q) Which of the following are valid Python
identifiers?
1) 123total
2) total123
3) java2share
4) ca$h
5) _abc_abc_
6) def
7) if
Lines and Indentation
Python does not use braces({}) to indicate blocks
of code for class and function definitions or
flow control. Blocks of code are denoted by
line indentation, which is rigidly enforced.

if True:
print ("True")
else:
print ("False")
Quotation in Python
Python accepts single ('), double (") and triple
(''' or """) quotes to denote string literals, as
long as the same type of quote starts and ends
the string.
The triple quotes are used to span the string
across multiple lines. For example, all the
following are legal
Quotation in Python
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up
of multiple lines and sentences."""
Comments in Python
A hash sign (#) that is not inside a string literal is
the beginning of a comment.

All characters after the #, up to the end of the


physical line,are part of the comment and the
Python interpreter ignores them.
Comments in Python
#First comment
print ("Hello, Python!") # second comment
Multiple Statements on a Single Line
The semicolon ( ; ) allows multiple statements
on a single line given that no statement starts a
new code block.

Here is a sample snip using the semicolon


import sys;
x = 'foo'; sys.stdout.write(x + '\n')
Multiple Statement Groups as Suites
Groups of individual statements, which make a
single code block are called suites in Python.
Compound or complex statements, such as if,
while, def, and class require a header line and
a suite.
Multiple Statement Groups as Suites
Header lines begin the statement (with the keyword)
and terminate with a colon ( : ) and are followed
by one or more lines which make up the suite.
For example −
if expression :
suite
elif expression :
suite
else :
suite
Variable Types
Variables are nothing but reserved memory locations
to store values.
It means that when you create a variable, you
reserve some space in the memory.

Based on the data type of a variable, the interpreter


allocates memory and decides what can be stored in
the reserved memory.

Therefore, by assigning different data types to the variables


, you can store integers, decimals or characters in these
variables.
Assigning Values to Variables
Python variables do not need explicit declaration to
reserve memory space.
The declaration happens automatically when you
assign a value to a variable.

The equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name


of the variable and the operand to the right of the =
operator is the value stored in the variable.
Assigning Values to Variables
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print (counter)
print (miles)
print (name)

Here, 100, 1000.0 and "John" are the values assigned to counter,
miles, and name variables, respectively. This produces the
following result −
100
1000.0
John
Multiple Assignment
Python allows you to assign a single value to
several variables simultaneously.
example
a=b=c=1

Here, an integer object is created with the value


1, and all the three variables are assigned to the
same memory location. You can also assign
multiple objects to multiple variables.
Multiple Assignment
For example
a, b, c = 1, 2, "john"

Here, two integer objects with values 1 and 2 are


assigned to the variables a and b respectively,
and one string object with the value "john" is
assigned to the variable c.
Data Types

• Data Type represents the type of data


present inside a variable.
• Python is dynamically Typed Language.
• Based on value provided, the type will be
assigned automatically
Data Types
Python Numbers

int
a=5
print(a, "is of type", type(a))
Float
a = 2.0
print(a, "is of type", type(a))
Python Strings

• String is sequence of Unicode characters. We can use


single quotes or double quotes to represent strings.
• Multi-line strings can be denoted using triple quotes, ''' or
""".
Example
s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
Python Strings

• Strings are immutable


Example
s = "This is a string"
s[2]= “2"
print(s)
Python List

• List is an ordered sequence of items.


• All the items in a list do not need to be of the
same type.
• Declaring a list is pretty straight forward. Items
separated by commas are enclosed within
brackets [ ].
• Similar to Array in C Language
a = [1, 2.2, 'python']
Python List

• We can use the slicing operator [ ] to extract an item or a


range of items from a list. The index starts from 0 in
Python.
• a = [5,10,15,20,25,30,35,40]
• # a[2] = 15
print("a[2] = ", a[2])
• # a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])
• # a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
Python List

• Lists are mutable, meaning, the value of


elements of a list can be altered.
a = [1, 2, 3]
a[2] = 4
print(a)
Python Tuple

• Tuple is an ordered sequence of items same as a list.


• The only difference is that tuples are immutable.
Tuples once created cannot be modified.
• Tuples are used to write-protect data and are
usually faster than lists as they cannot change
dynamically.
• It is defined within parentheses () where items are
separated by commas.
• t = (5,'program', 1.33)
Python Tuple

t = (5,'program', 1+3j)
# t[1] = 'program‘
print("t[1] = ", t[1])
# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
t[0] = 10
Python Set

• Set is an unordered collection of unique


items. Set is defined by values separated by
comma inside braces { }.
a = {5,2,3,1,4}
# printing set variable
print("a = ", a)
# data type of variable a
print(type(a))
Python Set

• We can perform set operations like union, intersection on two


sets.
• Sets have unique values.
• They eliminate duplicates.

a = {1,2,2,3,3,3}
print(a)
Since, set are unordered collection, indexing has no meaning.
Hence, the slicing operator [] does not work.
a = {1,2,3}
a[1]
Python Dictionary

• Dictionary is an unordered collection of key-


value pairs.
• It is generally used when we have a huge
amount of data.
• Dictionaries are optimized for retrieving data.
We must know the key to retrieve the value.
Python Dictionary

• In Python, dictionaries are defined within braces {} with


each item being a pair in the form key:value.
• Key and value can be of any type.
KEY Value

d = {101:‘AAA',102:‘BBB',103:‘CCC'}
• Duplicate keys are not allowed but values can be
duplicated.
• If we are trying to insert an entry with duplicate key then
old value will be replaced with new value.
Python Dictionary

d = {101:‘AAA’,102:’BBB’,103:‘CCC’}
d[101]=‘aaa’
Print(d)
We use key to retrieve the respective value. But
not the other way around.
Print(d[‘aaa’])
Range Data Type:
• range Data Type represents a sequence of numbers.
• The elements present in range Data type are not modifiable. i.e range Data type is immutable.
• Form-1:
range(10)
generate numbers from 0 to 9
Eg: r = range(10)
for i in r : print(i)

Form-2: range(10, 20)


generate numbers from 10 to 19
Eg:
r = range(10,20)
for i in r : print(i)
10 to 19
Python Type Conversion and Type Casting

• 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.
Implicit Type Conversion
Explicit Type Conversion
Python Type Conversion and Type Casting

Implicit Type Conversion


• Python automatically converts one data type to another data type.
• This process doesn't need any user involvement.
• Conversion of the lower data type (integer) to the higher data type
(float) to avoid data loss.
• Example 1: Converting integer to float
num_int = 123
num_flo = 1.23
print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))
num_new = num_int + num_flo
print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
Python Type Conversion and Type Casting

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)
Python Type Conversion and Type Casting

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy