Python Fundamentals
TOKENS:
Keywords (Predefines words with special meaning to Python e.g. print, form,, while, if, list, dict, True, None etc.)
Identifiers (Names given to different parts of the programs viz. variables, functions, objects, classes, lists, dictionaries etc.)
Literals (Values e.g. 4, 100, 68.9, "Hello" , "Abhilash" etc.)
Operators (That triggers some computation, action e.g. + , / ,// , %, - , *, ** etc.)
Punctuators ( Symbols used in programming language to organize statement structure e.g. ' " # ( ) [ ] ; etc. )
EXPRESSIONS:
An expression is any valid combination of operators, literals and variablese.g.3 + 6 / 2 - 10%3 (Arithmetic expression)
x > y != z (Relational expression)
A and not B or C (Logical Expression)
Types of Operators:
- Arithmetic Operators (* + % // / etc.)
- Relational Operators (< > <= == != >=)
- Logical Operators (and or not)
- Membership Operators (in not in)
- Identity Operators (is is not)
OPERATORS PRECEDENCE:
Rules for identifier names:
Keywords and operators are not to be used Start with an alphabet or an underscore. Special characters other than underscores are allowed. Space is not allowed. Cannot start with a number. Note: Python is case sensitive and hence uppercase and lowercase are treateddifferently.
engmark, _abc , mark1, mark2
Mutable and Immutable types:
TYPE CASTING
The conversion of one data type into the other data type is known as Type Casting (also called Type Conversion).
There are two types of type conversion in Python
1. Implicit conversion - automatic type conversion
2. Explicit conversion - manual type conversion
Example:
x = '12' (Implicit) - Automatically converted to String
x = int('12') (Explicit) - Converted to integer by the programmer
Multiple Assignments:
a = b = c = 10
a,b,c = 1,2,"hello"
Types of Errors:
Syntax Errors: Syntax errors are mistakes in the source code, such as spelling and punctuation errors, incorrect labels, and so on, which cause an error message to be generated by the compiler/ interpreter.
Some of the examples of Syntax errors are missing semicolons · unbalanced parenthesis · missing operators · indentation · error etc.
Run time Errors: A runtime error in a program is one that occurs after the program has been successfully compiled.
- Some examples of Python Runtime errors
- division by zero
- using an identifier that has not been defined
- accessing a list element, dictionary value, or object attribute which doesn’t exist
- trying to access a file that doesn’t exist
Logical Errors: A logical error occurs in Python when the code runs without any syntax or runtime errors but produces incorrect results due to flawed logic in the code.
FLOW OF CONTROL
Selection Statement:
Iteration / Looping Statement:
Syntax: Nested loops (loop within loop):
JUMP Statements:
break
continue
Here, Statement 1, 2 and 3 will be executed. As soon as the program executes continue statement, the program restarts while loop.
STRING
S | P | y | t | h | o | n | i | s | e | a | s | y | ||
Positive Indexing | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
Negative Indexing | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
In-built String Methods:
string.count(str, beg=
0,end=len(string)) |
Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given. |
string.find(str, beg=0 end=len(string)) | Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise. |
string.index(str, beg=0, end=len(string)) | Same as find(), but raises an exception if str not found. |
string.isalnum() |
Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise. |
string.isalpha() | Returns true if string has at least 1 character and all characters are alphabetic and false otherwise. |
string.isdigit() |
Returns true if string contains only digits and false otherwise. |
string.isnumeric() | Returns true if a unicode string contains only numeric characters and false otherwise. |
len(string) | Returns the length of the string. |
string.split(str="", num=string.count(str)) | Splits string according to delimiter str (space if not provided) and returns list of substrings; split into at most num substrings if given. |
LIST
List Functions:
len(L) | returns the size (number of items) of the list |
list() | a Python built-in class that creates a list out of an iterable object passed as an argument. |
type(list) | returns the class type of an object |
L.append(value) | Adds a single element to a list. |
L.extend(List2) | Adds elements of List2 to L |
L.index(value) | Returns the first appearance of the specified value. |
max(L) | returns an item from the list with max value |
min(L) | It returns an item from the list with min value. |
L.clear() | Removes all the items from list |
L.count(value) | Returns the number of times an element occurs in the list. |
L.insert(index, value) | Inserts an item at a given position. |
L.remove(value) | Removes the first occurrence of the specified item from the list |
L.reverse() |
Reverses the index positions of the elements in the list. |
L.sort() |
Sorts the list items in ascending, descending, or in custom order. |
L.pop() or L.pop(index) |
Removes and returns the item from last position. |
TUPLE
Tuple is a collection of elements which is ordered and unchangeable (Immutable).DICTIONARY
Dictionaries are used to store data values in key:value pairs.A dictionary is a collection which is unordered, changeable and do not allow duplicates.
Dictionary Functions:
len(d) |
Find the length of a dictionary |
d.clear() |
removes all elements from the
dictionary |
d.get(key) |
Returns value of a key. |
d.keys() |
Returns all the keys in the form
of a list. |
d.values() |
Returns all the values in the
form of a list. |
d.items() |
Returns all the items in the form
of a list. |
d1.update(d2) |
Merges two dictionaries. Already
present elements are override. |
d.pop(key) |
Deletes the specified item from the dictionary |
No comments:
Post a Comment