1-Module-1 - Python Programming Fundamentals-04-01-2024
1-Module-1 - Python Programming Fundamentals-04-01-2024
PYTHON Programming
– Compilers.
• But for anything more than a few lines, should save your
code as a script so you can modify and execute it in
future.
Python….Script Mode …
File name : first.py
print(4+3)
print(4-3)
print(4>3)
print("hello World")
Problem
• Little Bob loves chocolate, and he goes to a
store with Rs. N in his pocket. The price of
each chocolate is Rs. C. The store offers a
discount: for every M wrappers he gives to the
store, he gets one chocolate for free. This offer
is available only once. How many chocolates
does Bob get to eat?
What is an Identifier?
• An identifier is a sequence of one or more
characters used to name a given program
element.
• In Python, an identifier may contain letters
and digits, but cannot begin with a digit.
• Special underscore character can also be used
• Example : line, salary, emp1, emp_salary
Rules for Identifier
• Python is case sensitive, thus, Line is different from
line.
• Identifiers may contain letters and digits, but cannot
begin with a digit.
• The underscore character, _, is also allowed to aid in
the readability of long identifier names. It should not
be used as the first character
• Spaces are not allowed as part of an identifier.
Identifier Naming
Keywords
• A keyword is an identifier that has pre-defined
meaning in a programming language.
• Therefore, keywords cannot be used as “regular”
identifiers. Doing so will result in a syntax error,
as demonstrated in the attempted assignment to
keyword and below,
Keywords in Python
Variables and Identifiers
• A variable is a name (identifier) that is
associated with a value.
• A simple description of a variable is “a name
that is assigned to a value,”
•
Variables are assigned values by use of the assignment operator,
num = 10
num = num + 1
Comments
• Meant as documentation for anyone reading the code
• Single-line comments begin with the hash character ("#")
and are terminated by the end of line.
• Python ignores all text that comes after # to end of line
• Comments spanning more than one line are achieved by
inserting a multi-line string (with """ as the delimiter one
each end) that is not used in assignment or otherwise
evaluated, but sits in between other statements.
• #This is also a comment in Python
• """ This is an example of a multiline comment that spans
multiple lines ... "“”
Literals
Since numeric literals without a provided sign character denote positive values,
an explicit positive sign character is rarely used.
String Literals
• String literals, or “strings,” represent a
sequence of characters,
'Hello' 'Smith, John' "Baltimore, Maryland
21210“
• In Python, string literals may be surrounded
by a matching pair of either single (') or
double (") quotes.
• >>> print('Welcome to Python!')
>>>Welcome to Python!
String Literal Values
Note
Control Characters
• Special characters that are not displayed on the screen.
Rather, they control the display of output
• Do not have a corresponding keyboard character
• Therefore, they are represented by a combination of
characters called an escape sequence.
• The backslash (\) serves as the escape character in
Python.
• For example, the escape sequence '\n', represents the
newline control character, used to begin a new screen
line
Data Types
• Python's data types are built in the core of the
language
• They are easy to use and straightforward.
• Data types supported by Python
– Boolean values
– None
– Numbers
– Strings
– Tuples
– Lists
– Sets
Boolean values
• Primitive data type having one of two values: True
or False
• some common values that are considered to be
True or False
Boolean values
print (bool(True)) True
print (bool(False)) False
print (bool("text")) True
print (bool("")) False
print (bool(' ‘)) False
print (bool(0)) False
print (bool()) False
print (bool(3)) True
print (bool(None)) False
None
>>>print(‘\n’*15)
# prints new line character fifteen times
Complex Numbers
• A complex number consists of an ordered pair
of real floating point numbers denoted by a +
bj, where a is the real part and b is the
imaginary part of the complex number.
• complex(x) to convert x to a complex number
with real part x and imaginary part zero
• complex(x, y) to convert x and y to a complex
number with real part x and imaginary part y.
• x and y are numeric expressions
Complex Numbers
>>> 1j * 1J
(-1+0j)
>>> 2 + 1j * 3
(2+3j)
>>> (2 + 1j) * 3
(6+3j)
• A = 1+2j; B=3+2j
• # Multiple statements can be given in the same
line using a semicolon
• C = A+B; print(C)
Complex Numbers
# prints real part of the number
• print(A.real)
# prints imaginary part of the number
• print(A.imag)
# Can do operations with part of complex number
• print(A.imag+3)
Input and output function
Input function : input
Basic_pay = input('Enter the Basic Pay: ')
Here, the entered number of credits, say '24', is converted to the equivalent
integer value, 24, before being assigned to variable num_credits. For input of the
gpa, the entered value, say '3.2', is converted to the equivalent floating-point
value, 3.2. Note that the program lines above could be combined as follows,
Assignment Statement
Statement Type
spam = 'Spam' Basic form
spam, ham = 'yum', 'YUM' Tuple assignment (positional)
[spam, ham] = ['yum', List assignment (positional)
'YUM']
a, b, c, d = 'spam' Sequence assignment,
generalized
a, *b = 'spam' Extended sequence unpacking
(Python 3.X)
spam = ham = 'lunch' Multiple-target assignment
spams += 42 Augmented assignment
(equivalent to spams = spams +
42)
Range
>>>a,b,c = range(1,4)
>>>a
1
>>>b
2