Python
Commands
Notes
cd = current directory
dir = all contents of my directory
cd .. = to go back from a directory
cls = to clear the screen
there are 3 main types of environment
1)text editor
2)full IDEs (use for large programs) eg – spider
3)Notebook environment
To print a line
print('hello world this is ok')
NAMES TYPE DESCRIPTION
Integer int 3,300,200
Floating point float 2.3,4.5
string str Order sequence of char – hello,200
lists list Ordered sequence of object –
10,”hello”, 200.3
dictionaries dict Unordered key (value pair) –
“mykey”:”value” , “name”:”op”
tuple tup Ordered immutable sequence of
object –(10,”hello”,200.9)
Sets set Unordered collection of unique
objects –(“a”,”b”)
**********************************************************************************
# python is a static type of language which means we can change the assign value at any line of code
Eg – int my_dog=1;
My_dog =”Sammy”;
Strings - strings are sequences of character ,
using the syntax of either single quote or
double quotes:
Eg – ‘hello’, “Hello”
Indexing is used if you want single character
Slicing allows you to grab a subsection of
multiple character, a ”slice” of the string
Syntax : [start:stop:step]
\n is for new line
\t is for tab (1 tab = 4space)
len is used to check the length of the
string eg . len(‘hello’)
5
Pov – space will be counted
INDEXING : eg –
Pov – this index start with 0 Pp =”hello”
Till n-1 Pp[2]
l (output)
negative indexing – start from backward
i.e - -1 to n
SLICING – to grab more than 1 char
Syntax = variable[:::]
Eg – pp=”123456789”
pp[ :3]
output – 123
note – because stop value is given so n-1
pp =”123456789”
pp[::2]=13579
IMMUTABILITY – mean mutate or cannot
change
Eg – name = “sam”
name[0] = ‘p’ we cannot do that
to change the value of string we have to
create a new string
last_letters = name[1:](we got the last 2
words)
‘P’ + last_letter
‘Pam’ – output
eg – x=’Hello World’
x + “it is okey”
Hello Worldit is okey = output ( no space
before ‘ and no space after “)
If you execute this line 2 time then same
line will be assigned to x two times
Letter = ‘z’
Letter * 10 = zzzzzzzzz(10 times)
NOTE – CAN’T BE DONE THIS WITH NO.
Eg – 2+3
5
‘2’+’3’
‘23’
SOME BUILDIN STRING METHOD
1. x=’Hello World’
x.upper()
‘HELLO WORLD’
Here all the letters are turned in upper
case and wise versa for ‘lower()’
2. x.split()
[‘Hello’ , ’World’]
So it will split all the words and we can
also split the world by removing a specific
word for eg.
X.split(‘i’)
[‘H’,’ th’,’s ‘,’s a str’,’ng’]
STRING FORMATTIG FOR PRINTING
Video no. - 19
There are 2 method for this
.format() method
F-strings(formatted string literals
.FORMAT() METHOD
A good way to format objects into your
string for print statement is with the
string .format() method. The syntax is
‘String here{} then also
{}’.format(‘word 1’,’word2’)
Eg.
1) print(‘this is a string{}’.format(‘INSERTED’))
output = This is a string INSERTED
2)print(‘the {} {} {}’.format(‘fox’,’brown’,’quick’))
Output = the fox brown quick
WE CAN ALSO ENTER VALUED USING INDEX NO.
print(‘the {2} {1} {0}’.format(‘fox’,’brown’,’quick’))
Output = the quick brown fox
We can repeat this also by putting
print(‘the {0} {0} {0}’.format(‘fox’,’brown’,’quick’))
Output = the fox fox fox
We can also set keyword for this
print(‘the {q} {b}
{f}’.format(f=’fox’,b=’brown’,q=’quick’))
output =the quick brown fox
Float formatting “{value:width.precision f}”
Eg = result = 100/777
Output = 0.1287001287001287
Print(“the result was {r :1.3f}”.format(r=result))
Output = The result was 0.129
WE CAN SKIP THIS FORMAT. THING BY NEW METHOD
Name=”pp”
Print(f ‘hello, his name is {name}’)
Output = hello , his name is jose
Name = “sam”
Age = 3
Printf(f ‘{name} is {age} years old.’)
Output = Sam is 3 year old.
To find no. of element in array= len(name)
Eg. My_list =[1,2,3]
Len(my_list) =3
We can also add element in list using
mutation.
Here on index 0 already ‘one’ was there which is replaced by
‘one ALL CAP’.(we added new no. at first now we will add at
last)
USE OF APPEND: it is used to add element at the last index
We were using append to add item in the list now HOW TO
REMOVE IT ? using “pop”.
Pop will delete the last element from the list and will change
the list. Give index inside brackets to remove any other item.
HOW TO SORT THE ITEM IN LIST USING FUNCTION ”SORT”
We can reverse it also using keyword reverse()
Eg. num_list.reverse()
DICTIONARIES
Dictionaries are unordered mapping for storing
objects. Previously we saw how lists store object
in an ordered sequence , dictionaries use a key
value pairing instead.
This key value pair allow user to quickly grab
objects without needing to know an index
location.
SYNTAX : LISTNAME ={‘key1’:value,’key2’:value2}
It is possible to enter lists in the list and we can call it normally
and even complete dictionaries can also be inserted.
Here in line 28 we used another [] to print the actual value
inside the dictionary.
WE CAN ALSO CHANGE THE VALUE FOR EXAMPLE
We can print all keys and value using this commands
Question: given d={‘k1’:[1,2,3]} then output of d[‘k1’][1]
Here k1 is the key but [1] is representing the indexing no. so
answer will be 2
TUPLES
Tuples are very similar to lists,however they have one key
difference – immutability.Once an element is inside a tuple,it
can not be reassigned . tuple ue parenthesis: (1,2,3).
Eg.- t = (1,2,3) eg.- mylist= [1,2,3]
This is a a tuple this is a list
we can use t.count(‘alphabet to search’)
SETS
Sets are unordered collection of
unique elements. Meaning there can
only be one representative of the
same object. Let’s see some example
If we add a number in set we have to
use setname.add(input)
Note – inputs should be unique or in result it will not be
included
BOOLEANS
Booleans are operators that allow you to convey TRUE or
FALSE statement.
These are very important later on when we deal with
control flow and logic
If else statement
If condition :
statement
elif condition:
statement
else :
statement
looping statements
while _condition :
statement
for _keyword_ in _array or string name
break: breaks out the current closest
enclosing loop
continue : goes to the top of thr closest
enclosing loop
pass : does nothing at all.
Eg:
Enumerates : Enumerate() method adds a counter to an
iterable and returns it in a form of enumerating object. This
enumerated object can then be used directly for loops or converted
into a list of tuples using the list() function.
ZIP: zip will zip all the given data like a zip of jacket
IN : if we wants to check something is present in list or not
then we can use in operator
SUFFLE : it is used to shuffle the list
RANDINT: it is used to generate random interger from the
list