0% found this document useful (0 votes)
7 views30 pages

Python Chapter 7 - Python Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
7 views30 pages

Python Chapter 7 - Python Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 30

PYTHON

FUNDAMENTALS
PYTHON CHARACTER SET
Character set is a set of valid characters that a language can recognize.
A character represents-
• Letters (A-Z),(a-z)
• Digits (0-9)
• Special symbols (space + - * / ** \ ( ) [ ] { } // = != == < , > ‘ “ ‘’’ ; , : % !
• Whitespaces Blank Spaces, tabs, carriage returns, newline, form feed.
• Other characters Python can process all ASCII and UNICODE characters
as a part of data and literals.
TOKENS
• Tokens are the smallest unit of the program.
• Python interpreter reads code as a sequence of bytecodes.
• Python translates characters into tokens, each corresponding to one lexical category.
• This process is called tokenization.
KEYWORDS
• Keywords are nothing but a set of special words, which are reserved by Python and have specific
meaning.
• Sometimes it may be a command, or a parameter etc. We cannot use keywords as variable names.
• 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.
• Keywords in Python are case sensitive.
• There are 35 keywords in Python 3.10. This number can vary slightly over the course of time.
• All the keywords except True, False and None are in lowercase and they must be written as they
are. The list of all the keywords is given below.

True False class def Return if elif


else try except raise finally for in
is not from import global lambda nonlocal
del pass while break continue and with
as yield or assert None
IDENTIFIERS
• An identifier is a name given to entities like class, functions,
variables, etc. It helps to differentiate one entity from another.
• Rules for writing identifiers
– 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 _.
– Identifiers cannot begin with a digit.
– Names like myClass, var_1 and print_this_to_screen, all are valid example.
– Keywords cannot be used as identifiers.
– There are 35 keywords in Python 3.8. This number can vary slightly over
the course of time.
– An identifier cannot start with a digit. 1variable is invalid, but variable1 is a
valid name.
LITERALS
Literal is a raw data given in a variable or constant.
In Python, there are various types of literals they are as follows:
BOOLEAN LITERAL
A Boolean literal can have any of the two values: True or False.

Example 8: How to use boolean literals in Python?


x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
NUMERIC LITERALS
Numeric Literals are immutable. Numeric literals can belong to following three different
numerical types.

Int(signed integers) float(floating point) Complex(complex)

Numbers( can be both Real numbers with both In the form of a+bj where a
positive and negative) with integer and fractional part forms the real part and b
no fractional part.eg: 100 eg: -26.2 forms the imaginary part of
the complex number. eg:
3.14j
NUMERIC LITERALS
Number System Prefix
Example – Integer Literals Binary '0b' or '0B'
x = 0b10100 #Binary Literals
y = 100 #Decimal Literal Octal '0o' or '0O'
z = 0o215 #Octal Literal
u = 0x12d #Hexadecimal Literal Hexadecimal '0x' or '0X'

Example – Float Literals


We can use the type() function to know which class a
float_1 = 100.5
float_2 = 1.5e2 variable or a value belongs to and isinstance() function to
check if it belongs to a particular class.
Example – Complex Literals isinstance(4,int)
a = 5+3.14j

print(x, y, z, u)
print(float_1, float_2)
print(a, a.imag, a.real)
TYPE CONVERSION
We can convert one type of number into another.
We can also use built-in functions like int(), float() and complex() to convert between types
explicitly.
These functions can even convert from strings.
STRING LITERALS
• A string is a sequence of characters.
• A character is simply a symbol. For example, the English language has 26 characters.
• Computers do not deal with characters, they deal with numbers (binary). Even though you may
see characters on your screen, internally it is stored and manipulated as a combination of 0s and
1s.
• This conversion of character to a number is called encoding, and the reverse process is
decoding. ASCII and Unicode are some of the popular encodings used.
• In Python, a string is a sequence of Unicode characters. Unicode was introduced to include
every character in all languages and bring uniformity in encoding.
• Strings can be created by enclosing characters inside a single quote or double-quotes. Even
triple quotes can be used in Python to represent multiline strings.
ESCAPE SEQUENCE
TYPES OF STRINGS IN PYTHON
• Single Line - The Strings that are enclosed in single quotes (‘ ‘)or
double quotes (“ “). They must be terminated in one line
• MultiLine – Multiline strings can be created in two ways-
– By adding a backslash at the end of normal single or double quotes single
line text.
– By typing the text in triple quotes.
• To count number of characters in str object, you can use len() function:
>>> print(len('please anwser my question’))
SPECIAL LITERAL NONE
• The None keyword is used to define a null value, or no value at all.
• None is not the same as 0, False, or an empty string. None is a datatype of its own (None Type)
and only None can be None.
• None is a pre-defined keyword but because it works as a literal also, therefore considered as a
Special Literal.
COLLECTION LITERALS
• The collection is made up of different types of primitive datatype objects that are grouped
together to form a single entity.
• In Python, Collection includes List, Set, Tuple, and Dictionary, etc.
OPERATORS
The operator can be defined as a symbol which is responsible for a particular operation between two operands.
Python provides a variety of operators, which are described as follows.
• Unary Operators
– Unary Plus(+)
– Unary Minus (-)
– Bitwise Complement (~)
– Logical Negation (not)
• Binary Operators
– Arithmetic Operators (+ - * / % ** //)
– Comparison operators (> < >= <= <>)
– Assignment Operators(=, /=, +=, *=, %=, -=, **=. //=)
– Logical Operators( and, or)
– Relational Operators (>, <, >=, <=, ==, !=)
– Bitwise Operators (&, ^,|)
– Membership Operators (in, not in)
– Identity Operators( is, is not)
PUNTUATORS
Used to implement the grammatical and structure of a Syntax.
Following are the python punctuators.
BAREBONES OF A PROGRAM IN PYTHON
BAREBONES OF A PROGRAM IN PYTHON
• Expression : - which is evaluated and produce result. E.g. (20 + 4) / 4
• Statement :- instruction that does something. e.g
– a = 15
– print(“Time to say GoodBye")
• Comments : which is readable for programmer but ignored by python interpreter
– i. Single line comment: Which begins with # sign.
– ii. Multi line comment (docstring): either write multiple line beginning with #
– sign or use triple quoted multiple line. E.g.
• Function
– a code that has some name and it can be reused.e.g. keyArgFunc in above program
• Block & indentation : group of statements is block.
• Indentation at same level create a block.e.g. all Statement of SeeYou function
PRINTING IN PYTHON
print in Python is the standard function used to print the output to the console. The syntax of this
function is as follows:
SYNTAX:
print(value1, value2, …, sep = ‘ ‘ , end = ‘ n ‘, file = sys.stdout, flush = False)

Parameter Description
value1, value2,
The outputs that need to be printed. Can be more than one

An optional parameter used to specify how you want to separate the objects being printed.
sep
The default value of this is one whitespace ( ‘ ‘ ).
An optional parameter used to specify what is to be printed at the end of the output. The
end
default value is ‘n’
file An optional parameter with a write method. The default value is sys.stdout
An optional parameter used to specify if the output has to be flushed (True) or buffered
flush
(False). Its default value is False
PRINT EXAMPLES
SPECIF YING SEPARATOR

• Separator creates a partition between different objects that are present within the print
statement. The default value of this attribute is a whitespace character ( ‘ ‘ ). The user can
change the value of this operator as and when required.

In the above example, different objects are separated by a comma ( , ) rather than a whitespace
character in contrast to the previous example.

You can also adjust what you what to print at the end of the output.
USING THE END PARAMETER

The end parameter allows you to configure what you what to print at the end of the output. The
default value of this parameter is ‘n’ or the next line character.
VARIABLES
• A Python variable is a reserved memory location to store values.
• Every value in Python has a datatype.
• Variables can be declared by any name or even alphabets like a, aa, abc, etc. The type (string,
int, float etc.) of the variable is determined by Python
• Python is a dynamically typed language so we don’t have to declare the variables.
• Variables are created when first assigned.
• Variables must be assigned before being referenced.
• The value stored in a variable can be accessed or updated later.
• The interpreter allocates memory on the basis of the data type of a variable.
• Del() function is used to delete the variable.
VARIABLE HANDLING IN PYTHON
INPUT DATA IN PYTHON
The input() method reads a line from input, converts into a string and returns it.
The syntax of input() method is:
input([prompt])
The input() method takes a single optional argument:
prompt (Optional) - a string that is written to standard output (usually screen) without trailing
newline

Whatever you enter as input, the input() function converts it into a string. If you enter an integer value,
still it will convert it into a string. If you want to number input from a user, you need to perform type
conversion on the input value.
Chapter Ends

You might also like