You are on page 1of 7

Python Notes

There are some built in modules and pip and some we


have to download.
To download open command shell ad min and type pip
install and the pip name and it will be installed
First program
To create a new program-
 right click on your project
 select new
 select python program
 name the program
You should always start a program with print ()
 To import a module enter import and the name of
the pip
Using Python as a calculator
We can use python as a calculator. We can so some
calculations in python. We can do-
 Addition
 Subtraction
 multiplication
 Division
 Complex calculations
Comments
Comments are lines which we just want the other person
to read but not the computer.
To make a comment we use a # at the beginning.
We can also make multi comment line. To use that use
three inverted commas """ at start and at the end"""
To have 2 lines in a single line, add a comma and type
end="". You can add anything in between the "".
Escape Sequences
Escape sequence characters are characters which are
used in strings to make a new line or leave a tab etc.

To insert characters that are illegal in a string, use an


escape character.
An escape character is a backslash \ followed by the
character you want to insert.
An example of an illegal character is a double quote
inside a string that is surrounded by double quotes:
Example
You will get an error if you use double quotes inside a
string that is surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north."
To fix this problem, use the escape character \":
Example
The escape character allows you to use double quotes
when you normally would not be allowed:
txt = "We are the so-called \"Vikings\" from the
north."
Other escape characters used in Python:

Code Result

\' Single Quote

\\ Backslash
\n New Line

\r Carriage Return

\t Tab

\b Backspace

\f Form Feed

\ooo Octal value

\xhh Hex value

Variables, Datatypes and Typecasting


A Python variable is a reserved memory location to
store values. In other words, a variable in a python
program gives data to the computer for processing.
Every value in Python has a datatype. Different data
types in Python are Numbers, List, Tuple, Strings,
Dictionary, etc. Example var1= 22.
We can store string, numbers and floating points.
To know the datatype of the variable type
print(type(var and the name of the variable))
Typecast is a way of changing an object from one data
type to the next. It is used in computer programming
to ensure a function handles the variables correctly. A
typecast example is the transformation of an integer
into a string. There are three functions-
 str()
 float()
 int()
User Input
to take user input type- (ex)
inpnum= input()
First Quiz
print("Enter your first number")
num1 = input()
print("Enter your second number")
num2 = input()
print("Enter your third number")
num3 = input()
print("Enter your fourth number")
num4 = input()
print("Your answer when you add these 4 numbers is", int(num1)+int(num2)-
int(num3)*int(num4))

String slicing
We can store strings in containers example-
Mestr- "Lifebuoy is a good product"
print(Mestr)
WE can check the length of the string by typing-
print(len(mestr))
There is a function in python where we can slice the
strings in to parts. This is based on letters and also the
spaces. These slicing can be done by-
 print(mestr[0:4])- which gives the letters from l to e
which are life. We can also leave the space where
we write 0 as it comes from default.
 print(mestr[0:26:3])- gives us the letters in the
phrase but leaves 3 letters which will give us leoi ao
oc. If we leave it blank, by default there will be 1.
Other Functions
There are many other functions in python where we can
check, count etc. They are-
 print(mestr.endswith(t)) - checks if the string ends
with the letter in the brackets.
 print(mestr.isalnum()) - checks if the string is alpha
numeric
 print(mestr.isalpha()) - checks if it is alpha
 print(mestr.count(i)) - counts how many 'I's are
there in the string

You might also like