About Python:
The layers differences between C++&JAVA &Python:
A python program, sometimes called a script, is a sequence of definitions and commands.These definitions are evaluated and the commands are executed by the Python interpreter in something called the shell. Typically, a new shell is created whenever execution of a program begins. In most cases, a window is associated with the shell.
Python as a computer language:
1.Has similar procedural for as lower-level languages.
2.Has relaxed constraints on the syntax of a language.
3.It is an interpretive language(Handles code line by line for anything programmed in python)
Parts of a Program:
- Modules - A module is a library of pre-defined values and functions.(That means module is a .py file containing Python definitions and statements.)
- Expression - An expression is a combination of values and operations that a known result(LHS##Lift-hands side) eg.2+3
- Statement - A statement performs a task (A command often called a statement, instructs the interpreter to output )
- Whitespace - a character(space tab etc) that separates other characters
* Indentation - grouping or code block are marked by a set of indentations. A paragraph of code.
* Continuation - The “” symbol is used to continue a line of code. - Comments - using symbol “#”
While loop:
while booleanExpression(true\false):
code block
or
loop = 10
while(loop > 0):
print("Countdown is "), loop
loop -= 1
print("Blastoff!")
For loop:
for letter in "Cookies":
print letter
we get result:
C
o
o
k
i
e
s
Range function:
- used to create a collection of numbers
- range(min, max) or range (min, max, interval)
* min is the beginning of the number sequence
* max is the end of the number sequence
* interval(step) can be default to 1
* used with for loop
for i in range(1,10):##[1,10)
print (i)
The String Type:
- A collection of characters.
- A sequence of a characters.
- Uses symbol pairs:
- [ ✔️] ‘string’ [ ❌]'string"
- [ ✔️]“string” [ ❌]"string’
- String Representation: Characters are actually numbers encoded by the ASCII table.
- String as a sequence:
[C] [o] [o] [k] [i] [e] [s]
[0] [1] [2] [3] [4] [5] [6]
[-7] [-6] [-5] [-4] [-3] [-2] [-1] - It is possible to do partial or combination of indexes to manipulate a sequence:
string[:5] would be: “Cooki”
string[5:] would be: “es”
string[2:6] would be: “okie”
string[0:6:2] would be: “Coi”
String operations:
- The in operator - Is this string the subset of the other string?
String :
- String are marked with ’ ’ or " "
- String are immutable
- String can be concatenated
- String can be indexed and sliced
- Can be type-cast with a built-in str() function
- Can only contain ASCII characters
List :
- List are marked with []
- List are mutable( can be changed)
- List can be concatenated
- List can be indexed and sliced
- List can be created with a built-in list() function
- List can contain any data type
Tuples :
- Tuples are marked with()
- Tuples are immutable
- Tuples can be concatenated
- Tuples can be indexed and sliced
- Can be created with a built-in tuple() function
- Can contain any data type
Dictionaries :
- Dictionaries are marked with {key 1: value1, key2:value2}
- Dictionaries keys are immutable
- Dictionaries values are mutable
- Dictionaries cannot be concatenated
- Dictionaries cannot be indexed and sliced
- Can be created with a built-in dict() function
- Keys must be immutable or simple types
- Values can be any type
Sets :
- Sets are marked with set([])
- Sets are mutable
- Sets cannot be concatenated
- Sets cannot be indexed and sliced
- Can be created with built-in set() function
- Can contain any immutable data types or simple data types
- All elements are one of a kind: Uniqueness
x = set('runoob')
y = set('google')
print(x, y)
>>>{'b', 'r', 'u', 'o', 'n'}, {'e', 'o', 'g', 'l'}
print (x & y) #intersection
>>>{'o'}
print(x | y) #union
>>>{'b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'}
print(x - y) #difference set
>>>{'r', 'b', 'u', 'n'}
Pass By Value :
- Pass-By-Value-means the parameter values are not changed.
- True for simple data types(boolean, float, int )
- True for immutable data types
Pass by Reference :
- The parameter can be changed
- The parameter must be a collection
- The parameter must be mutable
- Lists, Dictionaries(The values, not the keys), and Sets can be pass-by-reference.
Class :
- A class is a new datatype, like creating a new int, float, or even a string.
- These datatypes have to be defined
- The keyword class is used to define a class, much the same way that def is used to define a function.
- The creation of a class for use, as in creating a variable, is called creating an instance.
self: How to write a command:
- self: refers back to the instance of the class
- The printout of an object is from the command descriptor __ str __
- __ str __ is a standard command that can be overridden.
def __str__(self):
result=format("%s, %d %s %d")\
%(self.day_of_week,self.day,self.name_of_month,self.year)
return result
Standard Object Commands
- __ init __()
* Creates a new instance of an object. - __ str __()
* Prints out the instance of an object. - Encapsulation - The data and functions are contained within a class
- Inheritance - The class can be built upon a parent class. Object is just the basic. New objects can be created from older objects.
- Polymorphism - Operators and commands can be overridden. Each object can be created to include its own addition and subtraction.
will update later