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

PYTHON-LESSON2

The document provides an overview of the fundamentals of Python, covering keywords, identifiers, literals, and the structure of a Python program. It explains the characteristics of different types of literals, including string, numeric, and boolean literals, as well as the importance of variables, assignments, and dynamic typing in Python. Additionally, it highlights the features of the print statement and the use of comments in Python code.

Uploaded by

anuritrj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PYTHON-LESSON2

The document provides an overview of the fundamentals of Python, covering keywords, identifiers, literals, and the structure of a Python program. It explains the characteristics of different types of literals, including string, numeric, and boolean literals, as well as the importance of variables, assignments, and dynamic typing in Python. Additionally, it highlights the features of the print statement and the use of comments in Python code.

Uploaded by

anuritrj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

FUNDAMENTALS OF PYTHON

INTRODUCTION TO PYTHON 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.
In Python, keywords are case sensitive.
IDENTIFIERS
• A Python identifier is a name used to identify a
variable, function, class, module or other object.
• 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 _ . ...
• First character must be a letter. _ is treated as letter.
• An identifier cannot start with a digit. But can be part
of it.
• Keywords cannot be used as identifiers.
• We cannot use special symbols like !, @, #, $, % etc.
(EXCEPT UNDERSCORE(-))
• An identifier can be of any length.
• Case Sensitive
• Eg for valid identifiers: Myfile, _ds,File123,chk
• Eg for invalid identifiers: data-rec, 29Loc, break, my.file
LITERALS/ CONSTANTS

In computer science, a literal is a notation for representing a fixed value in source code. In
contrast to literals, variables or constants are symbols that can take on one of a class of fixed
values, the constant being constrained not to change.
STRING LITERALS
• String – is an array of characters. (alphanumeric)
• Text enclosed in quotes forms a string in python.
• “ab” ”a” ‘ab123’ are all string literals.
• Quotes can be single, double or triple.
• Escape Sequences - Character combinations consisting of a backslash (\)
followed by a letter or by a combination of digits are called "escape sequences."
To represent a newline character, single quotation mark, or certain other
characters in a character constant, you must use escape sequences.
• Mostly used for producing non printable (non graphic) characters and some
special characters(characters used by the language as part of a syntax) in your
output screen.
ESCAPE SEQUENCES
\n– NEW LINE
TYPES OF STRINGS
single line and multiline
• Single line strings(Basic strings) : The strings which is enclosed with single or
double quotes are single line strings. They must end in one line.
• Valid string: str1=“welcome”
• Str2 = ‘To Python’
• Invalid single line string.
• Text1 = “Hello. // Will throw error because
World” // enter pressed without closing quote.
Text1 = “Hello\
World”
Even though written in 2 lines (seperated with a \) the string is considered as
continuous. i.e. the string will be printed as Helloworld in output screen.
Multiline String
• Multiline strings are created by typing the text in triple quotation marks
( no backslash needed at the end of line.)
• Eg:
str1 = ”””Hello
World.
Here I Come!!!”””
Print(str1)

• Output
Hello
World.
Here I Come!!!
SIZE OF STRINGS
• ‘\\’ – size is 1. (as \\ is an escape sequence)
• ‘abc’. - size is 3
• “\ab” – size is 2
• “Seema\’s pen” – size is 11
• ”Amy’s” – size is 5.
• (Python allows a single quote (without escape sequence format) in double quoted string and vice versa.
• Nstr = “’he\
ll\
O’’’. - size is 5. \ is not counted in the size of the string.
Nstr = ‘’’HE
LL
O’’’. - size is 7. as enter key(new line) is counted.

Str2= ‘a\
b\
c’. -size is 3. backslash is not counted.
SIZE OF STRINGS

• Using the len() method


• To calculate the length of a string in Python, you can use the built-
in len() method. It takes a string as a parameter and returns an
integer as the length of that string. For example len(“educative”) will
return 9 because there are 9 characters in “educative”.
NUMERIC LITERALS
• There are three types of literals: Python integer literals, Python floating point
literals and Python imaginary literals (complex number with 0.0 real value).
• int. - signed integers. Positive or negative whole numbers with no decimal
point.
• float - real numbers with decimal point and fractional part.
• Complex. – complex numbers take the form a+bj where a and b are floats and j
represents sqrt(-1) which is an imaginary number. (a)is the real part and b is the
imaginary part.
INTEGER LITERALS
• Decimal integer literals – integer literal consist of sequence of digits is decimal
integer. Eg: 1234, 41,-17
• Octal integer literals – A sequence of digits starting with 0o(digit 0 followed by
alphabet o) is an octal integer. (octal nos range from 0 to 7)
• Eg: valid octal literals - 0o765, 0o231
• Invalid octal literals – 0o879
• Hexa decimal integer literals – Sequence of digits preceded by 0x or 0X is taken
to be an hexadecimal integer. (range -digits 0 to 9 and letters A to F)
• Eg: decimal number 12 will be written as 0XC as hexadecimal integer.
FLOATING POINT LITERALS
• Floating point constants consist of whole number part , fractional part and an exponent(optional).
• A real constant must have one digit with the decimal point either before or after. May have + or –
sign preceding it. The exponent can be given as E or e with an integer value.
• Valid real literals : 2.0, 17.5, -13.0 , -0.00635, .3, 7.
• 13E05, 1.53E07,0.152E08, 123.0E08, 12.E3, .25E-5, 3.E3
• Invalid real literals : 7 – no decimal point
• 17/2 – invalid symbol /
• 17,250.26.6 – comma not allowed. Two decimal points used.
• 1.7E – no exponent value
• 0.16E2.3 – Exponent cannot be fractional
• 17,22E02 – comma not allowed
• Numeric values with commas are not considered as int or float value, rather python treats them
as a tuple.(sequence of values)
BOOLEAN LITERALS

Bytes literals are always prefixed with 'b' or 'B'; they produce an
instance of the bytes type instead of the str type.
SPECIAL LITERAL - None
• None literal is used to indicate absence of value.
• Also indicate end of lists in python.
• None is used to specify to that field that is not created.
• None is not the same as 0, False, or an empty string. None is a datatype of its own
(NoneType) and only None can be None.
val1=10
val2=None
print(val1)
print(val2)
• Output
10
None
BAREBONES OF A PYTHON
PROGRAM
• Expressions.
• Statements.
• Comments.( inline comments and multiline comments or docstrings)
• Blocks and Indentation.
• Functions.
EXPRESSIONS

In Python, operators are special


symbols that designate that some
sort of computation should be
performed. The values that an
operator acts on are called
operands. A sequence of operands
and operators, like a + b - 5 , is
called
an expression. Python supports
many operators for combining data
objects into expressions.
STATEMENTS
• Instructions written in the source code for execution are called
statements.
• There are different types of statements in the Python programming
language like Assignment statement, Conditional statement, Looping
statements etc.
• These all help the user to get the required output.
• For example, n = 50 is an assignment statement.
COMMENTS
• Single-line comments are created simply by beginning a line with the hash (#) character, and
they are automatically terminated by the end of line.
• Comments that span multiple lines – used to explain things in more detail – are created by
adding a delimiter (‘’’ ‘’’ or “”” “””) on each end of the comment are called docstrings.
• Eg:
• If b<5: # colon means the statement requires a block.
• This is inline comment as it starts in the middle of a statement.
• #this is a program to generate Fibonacci series
• This is single line comment.
• ‘’’ Multi line comments are useful
• for documentation purpose’’’
• This is a docstring.
VARIABLES AND ASSIGNMENTS

• A variable is a reserved memory location to store values in a programming


language.
• But in python variables do not have fixed locations. The location they refer to
changes every time their values change.
• lvalue : variables or objects to which you can assign a value or expression.
(comes in Lhs of an assignment statement)
• Rvalue: is the expression that come on the rhs of an assignment statement.
VARIABLES AND ASSIGNMENTS

• A variable is defined only when you assign some value to it. Using an
undefined variable in a statement causes an error called name error.
Print(x). - will throw an error because x is undefined.
Find the output:
• X=20 p,q=3,5
• Print(x) q,r = p-2,p+2
print(p,q,r)
• A=b=c=0. this statement assigns value 0 to a, b and c. x=10
• x,y,z=10,20,30. This will assign the values orderwise.i.e. x=10, y=20 y,y=x+2,x+5
and z=30. print(y)
x,x =20,30
• x,y = y,x. the values are swapped x is having y value and y is having x y,y=x+10,x+20
value. print(x,y)
• While assigning values through multiple assigments, python first
evaluates the RHS expression and then assigns to LHS.
• a,b,c = 5,10,7
• b,c,a = a+1,b+2,c-1
• Print(a,b,c)
• Output : 6,6,12
DYNAMIC TYPING

• When we declare a variable in C or alike languages, this sets aside an area of


memory for holding values allowed by the data type of the variable. The memory
allocated will be interpreted as the data type suggests. If it’s an integer variable the
memory allocated will be read as an integer and so on. When we assign or initialize
it with some value, that value will get stored at that memory location. At compile
time, initial value or assigned value will be checked. So we cannot mix types.
Example: initializing a string value to an int variable is not allowed and the program
will not compile.
• But Python is a dynamically typed language. It doesn’t know about the type of the
variable until the code is run. So declaration is of no use. What it does is, It stores
that value at some memory location and then binds that variable name to that
memory container. And makes the contents of the container accessible through
that variable name. So the data type does not matter. As it will get to know the type
of the value at run-time.
CAUTION WITH DYNAMIC TYPING

• IDENTIFY THE PROBLEM IN THE FOLLOWING CODE


• x=10
• y=0
• y=x/2
• x=‘Day’
• Y=x/2
FEATURES OF PRINT STATEMENT

• It auto converts the items to strings.


• For numeric expressions it first evaluates them and then converts into equivalent
string and print it.
• Inserts spaces between the items automatically. Default separator (sep argument)
is space.
• print(“Python”,”is”,”easy”) will print Python is easy
• Print(“Python”,”is”,”easy”,sep = “$”) will print Python$is$easy
• It appends a newline character at the end of the line defaultly.
• Print(“Welcome to pip”,end = “@”) will print Welcome to pip@. The cursor will
not advance to next line.
• An empty print () statement prints a blank line.
FEATURES OF PRINT STATEMENT

You might also like