1 fundamental of python notes for school
1 fundamental of python notes for school
Fundamental of Python
Introduction of Python
Python is an interpreter, interactive, object-oriented, and high-level programming language. It was created
by Guido van Rossum in 1991 at National Research Institute for Mathematics and Computer Science
Netherlands.
Features of Python
It is loosely typed Programming Language or Dynamically typed language with few keywords and
simple English-like structure and is easy to learn.
It is free open-source and portable language having a large repository of libraries.
It take less time to develop as python programs are typically 3-5 times shorter than equivalent java
programs.
It is easily compatible with other languages like c, c++, java, etc.
Python is a Case-sensitive language.
Python IDLE
The python IDLE tool offers an interactive and a more efficient platform to write your code in python.
(Integrated Development Learning Environment)
Python IDLE comprise Python Shell (Interactive mode) and Python Editor (Script mode).
● Interactive Mode: The Python commands are directly typed at >>> command prompt and as soon
as we press the enter key, the interpreter displays the results immediately, which is known as
displaying. (We cannot save file or command)
>>>10+50
60
● Script Mode: We can write multiple line of code here and get output and errors are running code.
We can open script mode using CTRL+N
Syntax of print () in python:
print(value1,value1,…………,sep=’ ‘, end=’ ‘)
Displaying Vs Printing: Getting output without using print function in interactive mode is called displaying,
And getting output using print () in interactive or script mode.
Different types of files in python: .py, .pyw, .pyc, .pyd, .pyo, .pyz
Python character set: Character set is a set of valid characters recognized by python. A character by letter,
digit or any other special symbol.
1. Letters: A-Z, a-z 2. Digits: 0-9 3. Special Symbols:+-/*&=!@_ etc.
4. Whitespaces: Blank space, tabs (‘\t’), carriage return (Enter key), newline, form feed (skips to the start
of the next page.)
5. Other Characters: All ASCII code and Unicode character.
Tokens: A token is the smallest element of a python script that is meaningful to the interpreter.
Variable:
A Variable is like a container that stores any value. x=3, age=30
Multiple Assignments:
1) Assigning multiple values to multiple variables: x,y,z=2,3,4
2) Assigning same value to multiple variable: a=b=c=10
a=10
a=”Hello”
Keywords
Keywords are words that are already reserved for some particular purpose. The names of these keywords
should not be used as identifiers/Variable. Keywords are also called reserved words.
False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global,
if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.
1. Implicit Type Conversion: Python automatically converts one data type to another data type. This
process doesn't need any user involvement. Ex: type(5/2)
2. Explicit (forced) Type Conversion: In Explicit Type Conversion, users convert the data type of an
object to required data type. Ex: float(5), str(5),int(‘5’)
Binary Operators: Operators that operate on two operands are known as binary operators.
Ex: 3+3
Unary Operators: Operators that operate on one operand are known as unary operators.
Ex: -3
Types of Operators:
1. Arithmetic operators: Plus + , Minus - , Multiply * , Division / , Floor Division / /, Modulus % , Power **
Example: 12 + ( 3 ** 4 – 6 ) / 2
2. Comparison operators: Equality == , Not Equal to !=, Less than < , Less than or Equal to <=, Greater than
>, Greater than Equal to >=
5. Membership operators:
‘H’ in ‘Hello’ => True ‘y’ in ‘Hello’ => False ‘HEL’ in ‘Hello’ => False
‘y’ not in ‘Hello’ => True ‘H’ not in ‘Hello’ => False
6. Identity operators,
Operators Meaning
() Parentheses
** Exponent (Power)
* , /, //, % Multiplication, Division, Floor division, Modulus
+- Addition, Subtraction
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators
Not Logical NOT
And Logical AND
or Logical OR
1. Sequence: In this, program executes in sequential order, one after another, without any jump in the
program.
a=10
b=20
c=a+b
print(c)
2. Selection/ Decision: In this, program executes according to condition. If any condition in true code will
run if condition if false code will not run. Keyword If , else
1. if statement
2. if else statement
3. if elif else statement
4. Nested if-else statement
if statement: if else statement:
The statements inside the body of “if” only The if/else statement executes a block of code if a
execute if the given condition returns true. If the specified condition is true. If the condition is false,
condition returns false then the statements inside another block of code can be executed.
“if” are skipped. Syntax:
Syntax: if test expression:
if test expression: Body of if
statement(s) else:
statement(s) Body of else
if elif else statement: Nested if-else statement:
The if-elif-else statement allows you to check When you want to check for another condition after
multiple expressions for TRUE and execute a block a condition resolves to true. In such a situation, you
of code as soon as one of the conditions evaluates can use the nested if else statement.
to TRUE. The syntax of the nested if...elif...else construct
Syntax may be −
if expression1:
statement(s) if expression1:
elif expression2: statement(s)
statement(s) if expression2:
elif expression3: statement(s)
statement(s) else:
else: statement(s)
statement(s) else:
statement(s)
3. Iteration / Looping: In this statement program executes multiple times according to condition. Keyword
while, for
4. Jump Statement:
str1=’Hello world’
str2=”Python Programming”
str3=’’’Python
Programming’’’
*(An escape sequence character is represented as a string with one byte of memory.)
Empty String: An empty string is a string without any characters inside. str=” ”, str=’ ‘
Multiple line String: Multiline strings are represented using triple quotes (‘’’ ‘’’) or even single or double
quotes.
Accessing individual characters of the string by using the index value is called indexing.
Traversing a String: Traversing a string means accessing all the elements of the string one after the other
by using the subscript / index value.
For loop:
While loop:
Repetition or Replicate: Create multiple copies of any string. * for repetition. Some Example: “Hello” *3.
Membership: Check whether a particular character exists in the given string or not.
Operators are: “in” and “not in”
Some Example:
“H” in “Hello” Gives True
“H” not in “Hello” Gives False.
Comparison Operators: Comparison operators are used to compare two strings. Python compares strings
using ASCII or UNICODE.
Some Example:
“Tim”==”tim” Gives False
“Freedom” > “Free” Gives True
String Slicing: Slicing is used to retrieve a subset of values. Chunk of characters can be extracted from a
string using slice operator. Example: var1 [start: end: step]
Some Example:
Str=”Save money”
str[1:3] gives “av”
str[:3] gives “Sav”
Updating: We can "update" an existing string by (re)assigning a variable to another string.
Declaring/Creating List:
Traversing a list: Traversing a list means accessing each element of a list. This can be done by using either
for or while looping statement.
Aliasing means: It mean nick name or alternate name of object. Means two object with same id.
Copying list: It means to create clone or copy the list as different object. Means with different id.
List Operators in Python
3) Membership Operator: ‘in’ And ‘not in’ are the operators for membership operator.
4) Indexing: Index is nothing but there is an index value for each item present in the sequence or list.
Example of nested list also.
6) Comparing List: Python allows us to compare two lists. Each element is individually compared in
lexicographical (alphabetical or dictionary) order.
Tuple in Python:
What is Tuple:
Tuple is a sequence of immutable Python object. Tuples are sequences, just like lists.
Tuples use parentheses and lists use square brackets.
Tuple can hold elements of different data type (Heterogeneous).
Tuples are immutable it means we cannot perform insert, update and delete operation on them. So,
iterating through a tuple is faster as compared to a list.
Tuple creation:
Traversing a tuple means accessing each element of a tuple. This can be done by using either for or while
looping statement.
What is Dictionary:
A python dictionary is a mapping of unique keys to values. It is a collection of key-value pairs. Dictionaries
are mutable which means they can be changed.