COMPUTER NOTES (Lect
COMPUTER NOTES (Lect
COMPUTER NOTES (Lect
FILE EXTENSION
INTERACTIVE MODE-
`to work in the interactive mode, we can simply type a python statement on the >>> promt directly.
as soon as we press enter, the interpreter executes the statement and displays the results(S)
working in the interactive mode in convenient for testing a single line code for instant execution.
but in the interactive mode, we cannot save the statements for future use and we have to retype the
statements to run them again**
SCRIPT MODE-
in the script mode we can write a python program in a file,save it and them use the interpreterto
exexute it.
python scripts are saved as files where file name has extension .py
by default, the python scripts are saved in the python installation folder.
STATEMENTS-
1. assingment statements.
2. conditional statements.
3. looping statements,etc.
in general, the interpreter reads and execute the statements line by line i.e sequentially and mostly.
python statements are written in such a format that one statement in only written in a single line.
the interpreter considers the 'new line character' as the terminator of one instructions.
but,writing multiple statements per line and multi-line statements is also possible.
some statements may become very long and may force us to scroll the sreen left and right frequently.
we can fir our code in such a way that we do not have to scroll here and there.
python allows us to write a single statement in multiple lines, also known as line continuation.
line continuation enhances readablility as well.
this is the most straight forward technique in writing a statement that spans multiple lines.
any statement containing opening parentheses '(', brackets'[', or curly braces'{' is presumed to be in
complete ultil all matching parentheses, square brakcets, curlty braces have been encountered.
until then, the statement can be implicitly continued across lines without raising an error.
explicit line joining is used mostly when implicit line joinig is not applicable. in this method, you have
to use a charactger that helps the interpreter to understand that the particular statements is spannig
more than one lines.
backslash(\) is used to indicate that a statements spans more than one line. the point is to be noted that
"__" must be the last character in that line, even white space in not allowed.
COMMENTS- (defn**_**)
NOTE- comments are not executed by interpreter,interpreter ignores the comments and does not count them
in commands.
python single line comments starts with hashtag symbol with no white spaces(#) and lasts till the end
of the line.
if the comment exceeds one line then put a hashtag on the next line and continue the comment.
python's single-line comments are proved useful for supplying short explanations for variables,
function declarations, and expressions.
MULTI-LINE STRING AS A COMMENT : (double inverted commas three times without space)
python multiline comment is a piece of text enclosed in a delimiter(""") on each end of the comment.
there should be no white space between delimeter(""").
they are useful when the comment text does not fit into one line,therefore need to span across lines.
nulti-line comments or paragraphs serve as documentation for others reading your code.
WHITE SPACES :
WHITESPACES AS INDENTATION :
MEANING OF INDENTATION :
KEYWORDS :
the keywords are some predefined and reserved words in python that have special meanings.
keywords are udes to define the syntax of the coding.
the keyword cannot be used as an identifier, function, and variable name.
**all the keywords in python are written in lower case-except True, False and None.**
for python 3.9.7, there are 35 kwywords in total and may change slightly with versions.
IDENTIFIERS :
the indentifier is a name used to identify a variable, function , class, module, etc.
it helps to differentiate one entity from another.
P.T.R :
VARIABLES :
NOTE : in python, we can't actually assign values to the variables. instead, python gives the reference of the
object (value) to the variable.
ASSIGNING VALUES TO VARIABLES IN PYTHON :
CONSTANTS :
NOTE : in reality, we don't use constants in python.naming them in all capital letters is a conversation to
sepereate them from variables , however , it does not actually prevent assingment.
PCONST LIBRARY : practically(27:18 lecture 11)
NOTE :
in pythons we can use an assignment statement to create new variables and assign specific values to
them.
variable declaration is implicit in python, means variablesn are automatically declared and defined
when they are assigned a value the first time.
variables must always be assigned values before they are used in expressions as otherwise it will lead
to an error in the program.**(question)
wherever a variable name occurs in an expression, the interpreter replaces it with the value of that
particular variable.
1. numeric literals
2. string literals
3. boolean literals
4. special literals
5. literal collections.
NUMERIC LITERALS :
STRING LITERALS :
a string literal is a sequence of characters surrounded by quotes. we can use bith single, double, or
triple quotes for a string.
and, a character literal is a single character surrounded by single or double quotes.
we'll see-
BOOLEAN LITERALS : a boolean literals can have any of the two values ;
1. true
2. false.
SPECIAL LITERALS :
LITERAL COLLECTIONS :
1. list literals
2. tuple literals
3. dict literals
4. set literals
DATA TYPES :
NUMERIC :
in python,numeric data type represent the data which has numeric value.
numeric value can be integer, floating number or even complex numbers.
these values are defined as int, float and complex class in python.
INTEGERS
FLOAT
COMPLEX NUMBERS
1. complex number is represented by complex class. it is specified as(real part) + (imaginary part)j. for
example- 2+3j.
BOOLEAN :
data type with one of the two built-in values, true or false.
boolean objects that are equal to true and truthy(true), and those equal to false are falsy(false).
but non-boolean objects can be evaluated in boolean context as well and determined to be true or false.
it is denoted by the class bool.
NOTE => true and false with capital 'T' and 'F' are valid booleans otherwise python will throw an error.
SEQUENCE TYPE :
1. string
2. list
3. tuple.
a string is a collection of one or more characters put in a single quote, double-quote or triple quote.
in python there is no character data type, a character is a string of length one.
it is represented by str class.
SEQUENCE TYPE-LIST :
list are just like arrays, declared in other languages whcih is a ordered collection of data.
it is very flexible as the items in a list do not need to be of the same type
CREATING LIST -list in pythons can be created by just placing the seuqnce inside the square brackets[ ]
TUPLE :
tuple is also an ordered collection of python objects. the only difference between tuple and list is that
tuples are immutable i.e. tuples cannot be modified after it is created.
it is represented by tuple class.
CREATING TOUPLE -tuples are created by placing a sequence of values seperated by 'comma' with or
without the use of parantheses( ) for grouping of the data sequence.
NOTE => tuples can also be created with a single element, but it is a tricky.having one element in the
parantheses is not sufficient, there must be a trailing 'comma' to make it a tuple.
NOTE =>creation of python tuple without the use of parantheses is known as tuple packing.
SETS :
in python, set is an unordered collection of data type that is iterable, mutable, and has no duplicate
elements.
the order of elements in a set is undefined though it may consist of various elements.
type of elements is a set need not be the same , various mixed-up data type values can also be passed
to the set.
CREATING SETS :
sets can be created by using built-in set ( ) function with an iterable object.
a sequence by placing the sequence crly braces,seperated by 'comma'.
dictionary in python is an unordered collection of data values, used to store data values like a map ,
which unlike other data types that hold only single value as an element.
dictionary holds (key:value)pair.
key-value is provided in the dictionary to make it more optimized.
each key-value pair in a dictionary is seperated by a colon(:)
each key is seperated by a comma(,).
CREATING DICTIONARY :
dictionary can be created by placing a sequence of elements eithing curly { } braces, seperated by
'comma'.
values in a dictionary can be of any datatype and can be duplicated, whereas keys can't be repeated and
must be immutable.
dictionary can also be created by the built-in function dict().
an empty dictionary can be created by just placing it to curly braces{}.
NOTE => Dictionary keys are case sensitive, same name but different cases of key will be treated distinctly.
type( ) and isinstance( ) functions :
(e.g) x=[1,2,3,4,5]
print(type(x))
(e.g) x = 2
isinstance(x, class)
MUTABLE AND IMMUTABLE DATA TYPES :
sometimes we may require to change or update the values of certain variables used in a program.
however, for certain data types, python does not allow us to change the values once a variable of that
type has been created and assigned values.
variables whose values can be changed after they are created and assigned are called mutables.
variables whose value cannot be changed after they are created and assigned are called immutable.
when an attempt is made to update the value of an immutable variable, the old variable is destroyed
and a new variable is created by the same name in mermory.
it is preferred to use lists when we need a simple iterable collection of data that may go for frequent
modifications.
for example- if we store the names of students of a class in a list, then it is easy to update the list when some
new students join or leave the course.
tuples are used when we do not need any change in the data.
when we need uniqueness of elements and to avoid duplicacy it is preferable to use sets.
for example- a mobile phone book is a good application of dictionary.{[ lecture 13 coding]}
#1:14:00#
TYPE CONVERSION:
type conversion is the method to convert the variable's dta types into a certain data types in order to
perform the operation required by the users.
1. in implicit type conversion, python automatically converts one data type to another data type.this
process doesn't need any user involvement.
2. data type conversion happen during complitation or on run time.
1. in explicit type conversion,users convert the data types of an object to required data type.
2. this type of conversion is also called typecasting because the user casts (changes) the dta type of the
objects.
PTR :-