Professional Documents
Culture Documents
Python
Python
First of all, I would like to tell you one thing - the main motive of using
Python is not to do raw calculations. You will perhaps never use python to
calculate 3+8 or 34*232 for that matter.
Yeah, that’s true! Python is not just limited to do calculations. It has a vast
number of uses and has many merits or advantages. Using Python you can
do most of the things, such as creating games, apps, software, websites,
etc.
And that’s how you can easily do any type of mathematical calculation in
Python. I hope you are enjoying this series of videos :)
Comments, Escape Sequences & Print Statement
Comments are used to write something which the programmer does not
want to execute. Comments can be written to mark author name, date
when the program is written, adding notes for your future self, etc.
Comments are used to make the code more understandable for the
programmer.
The Interpreter does not execute comments.
Single Line Comment: Single Line comments are the comments which
are written in a single line, i.e., they occupy the space of a single line.
import os
#This is a comment
print("Main code started")
We use ' ' '….. Comment ….' ' ' for writing multi-line comments in
Python (Use lines enclosed with three quotes for writing multi-line
comments). An example of a multi-line comment is shown below:
import os
'''This is a comment
Author: Harry
Date: 27 November 2020
Multi-line comment ends here
'''
print("Main code started")
Variable
A variable is a name given to any storage area or memory location in a
program.
Note – We can add (concatenate) two or more strings and the strings will
be concatenated to return another string. Heres the example showing that:
Typecasting :
Typecasting is the way to change one data type of any data or variable to
another datatype, i.e. it changes the data type of any variable to some
other data type.
I know it's a bit confusing but let me tell you in a simple manner. Suppose
there is a string "34" Note: String is not integer since it is enclosed in
double-quotes) and as we know we can't add this to an integer number let's
say 6. But to do so we can typecast this string to int data type and then we
can add 34+6 to get the output as 40. Have a look at the program below:
There are many functions to convert one data type into another type :
str() – this function allows us to convert some other data type into a string.
int() – this function allows us to convert some other data type into an
integer. For example, str("34") returns 34 which is of type integer (int)
float() – this function allows us to convert some other data type into
floating-point number i.e. a number with decimals.
input() Function – This function allows the user to receive input from the
keyboard into the program as a string.
input() function always takes input as a string i.e. if we ask the user to take
a number as input even then it will take it as a string and we will have to
typecast it into another data type as per the use case.
If you enter 45 when input() is called, you will get "45" as a string
Quick Quiz: Create a program that takes two numbers as input from the
user and then prints the sum of these numbers.
Solution :
Code file as described in the video
var1 = "54"
var4 = "32"
var2 = 4
var3 = 36.7
# print(100 * str(int(var1) + int(var4)) )
# print(100 * "Hello world\n")
# print("Enter your number")
# inpnum = input()
#
# print("You entered", int(inpnum)+10)
"""
str()
int()
float()
"""
"""
Quiz - Solved in the video
Exercise - Next video
Project - Some awesome python utility
"""
# print(type(var1))
Strings
String is a data type in Python. Strings in Python programming
language are arrays of bytes representing a sequence of characters. In
simple terms, Strings are the combination or collection of characters
enclosed in quotes.
Let us now look into some functions you will use to manipulate or perform
operations on strings.
len() Function : This len() function returns the total no. of characters in a
string. E.g. for string a="abc", len(a) will return 3 as the output as it is a
string variable containing 3 characters
Strings are one of the most used data types in any programming language
because most of the real-world data such as name, address, or any
sequence which contains alphanumeric characters are mostly of type
‘String’.
String Slicing :
As we know the meaning of the word ‘slice’ is ‘a part of’. I am sure you
have sliced paneer cubes at home!
Just like paneer slice refers to the part of the paneer cube; In Python, the
term ‘string slice’ refers to a part of the string, where strings are sliced
using a range of indices.
To do string slicing we just need to put the name of the string followed by
[n:m]. It means ‘n’ denotes the index from which slicing should start and ‘m’
denotes the index at which slicing should terminate or complete. Let's look
into an example!
Let's end this tutorial by looking into some of the most used string functions :
string.endswith(): This function allows the user to check whether a
given string ends with passed argument or not. It returns True or
False.
I hope you enjoyed this tutorial. If you have any questions, make sure to
post it in the QnA. Let us now move into the next topic Lists, which is also
one of the most used data types in the python programming language
print(mystr.endswith("bdoy"))
print(mystr.count("o"))
print(mystr.capitalize())
print(mystr.replace("is", "are"))
String Functions
Method Description
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where
index() Searches the string for a specified value and returns the position of where
isalpha() Returns True if all characters in the string are in the alphabet
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of wh
rindex() Searches the string for a specified value and returns the last position of wh
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
zfill() Fills the string with a specified number of 0 values at the beginning
Note: If you put an index which isn’t in the list i.e. that index is not there in
list then you will get an error. i.e. if a list named list1 contains 4 elements,
list1[4] will throw an error because the list index starts from 0 and goes upto
(index-1) or 3.
Have a look at the examples below:
List Methods :
Here is the list of list methods in Python. These methods can be used in
any python list to produce the desired output.
List Slicing :
List slices, like string slices, returns a part of a list extracted out of it. Let me
explain, you can use indices to get elements and create list slices as per
the following format :
seq = list1[start_index:stop_index]
Just like we saw in strings, slicing will go from a start index to stop_index-1.
It means the seq list which is a slice of list1 contains elements from the
specified start_index to specified (stop_index – 1).
List Methods:
There are a lot of list methods that make our life easy while using lists in
python. Lets have a look at few of them below:
Tuples in Python:
A tuple is an immutable data type in Python. A tuple in python is a
collection of elements enclosed in () (parentheses). Tuple once defined
can’t be changed i.e. its elements or values can’t be altered or
manipulated.
Note: To create a tuple of one element it is necessary to put a comma ‘,’
after that one element like this tup=(1,) because if we have only 1 element
inside the parenthesis, the python interpreter will interpret it as a single
entity which is why it’s important to use a ‘,’ after the element while creating
tuples of a single element.
List Function –
Method Description
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value