Just like we use Hindi or English to communicate with each other, we use a Programming language to communicate with the computer.
Programming is a way to instruct the computer to perform various tasks.
Python is a simple and easy-to-understand language that feels like reading simple English. This Pseudo code nature of Python makes it easy to learn and understandable for beginners.
Installation
Python can be easily installed from python.org
When you click on the download button, python can be installed right after you complete the setup by executing the file for your platform.
Tip: Just install it like a game ☻
Let’s write our very first python program.
Create a file called hello.py and paste the below code into it
Execute this file (.py file) by typing python hello.py, and you will see Hello World printed on the screen.
A module is a file containing code written by somebody else (usually) which can be imported and used in our programs.
Pip is a package manager for python. You can use pip to install a module on your system.
E.g., pip install flask (It will install flask module in your system)
There are two types of modules in Python:
Some examples of built-in modules are os, abc, etc.
Some examples of external modules are TensorFlow, flask, etc.
Using Python as a Calculator
We can use python as a calculator by typing “python” + TO DO on the terminal. [It opens REPL or read evaluation print loop]
Comments are used to write something which the programmer does not want to execute.
Comments can be used to mark the author's name, date, etc.
Types of Comments:
There are two types of comments in python,
A variable is a name given to a memory location in a program. For example
Variable – Container to store a value
Keywords – Reserved words in Python
Identifiers – class/function/variable name
Primarily there are the following data types in Python:
Python is a fantastic language that automatically identifies the type of data for us.
a = 71 #Identifies a as class<int>
b = 88.44 #Identifies b as class<float>
name = “Raushan” #Identifies name as class<Str>
Rules for defining a variable name: (Also applicable to other identifiers)
Examples of few valid variable names,
Raushan, raushan, one8, _akki, aakash, raushan_bro, etc.
The following are some common operators in Python:
type() function and Typecasting
type function is used to find the data type of a given variable in Python.
a = 31
type(a) #class<int>
b = “31”
type(b) #class<str>
A number can be converted into a string and vice versa (if possible)
There are many functions to convert one data type into another.
Str(31) # ”31” Integer to string conversion
int(“32”) # 32 String to int conversion
float(32) #32.0 Integer to float conversion
… and so on
Here “31” is a string literal, and 31 is a numeric literal.
input() function
This function allows the user to take input from the keyboard as a string.
a = input(“Enter name”) #if a is “Raushan”, the user entered Raushan
Note: The output of the input function is always a string even if the number is entered by the user.
Suppose if a user enters 34, then this 34 will automatically convert to “34” string literal.
The string is a data type in Python.
A string is a sequence of characters enclosed in quotes.
We can primarily write a string in three ways:
A string in Python can be sliced for getting a part of the string.
Consider the following string:
The index in a string starts from 0 to (length-1) in Python. To slice a string, we use the following syntax:
Negative Indices: Negative indices can also be used as shown in the figure above. -1 corresponds to the (length-1) index, -2 to (length-2).
Slicing with skip value
We can provide a skip value as a part of our slice like this:
word = “amazing”
word[1:6:2] # It will return ’mzn’
Other advanced slicing techniques
word = ‘amazing’
word[:7] or word[0:7] #It will return ‘amazing’
word[0:] or word[0:7] #It will return ‘amazing’
Some of the most used functions to perform operations on or manipulate strings are:
len(‘Raushan’) #Returns 7
Sequence of characters after backslash ‘\’ [Escape Sequence Characters]
Escape Sequence Characters comprises of more than one character but represents one character when used within the string.
Examples: \n (new line), \t (tab), \’ (single quote), \\ (backslash), etc.
letter = ‘’’ Dear <|NAME|>,
You are selected!
<|DATE|>
letter = “Dear Raushan, This Python course in nice. Thanks!!”
Python Lists are containers to store a set of values of any data type.
friends = [‘Apple’, ‘Akash’, ‘Rohan’, 7, False]
The list can contain different types of elements such as int, float, string, Boolean, etc. Above list is a collection of different types of elements.
A list can be index just like a string.
L1 = [7, 9, ‘Raushan’]
L1[0] – 7
L1[1] – 9
L1[70] – Error
L1[0:2] – [7,9] (This is known as List Slicing)
Consider the following list:
L1 = [1, 8, 7, 2, 21, 15]
A tuple is an immutable (can’t change or modified) data type in Python.
a = () #It is an example of empty tuple
a = (1,) #Tuple with only one element needs a comma
a = (1, 7, 2) #Tuple with more than one element
Once defined, tuple elements can’t be manipulated or altered.
Tuple methods:
Consider the following tuple,
a = (1, 7, 2)
a = (7, 0, 8, 0, 0, 9)
Dictionary is a collection of key-value pairs.
Syntax:
''' a = {“key”: “value”,
“Raushan”: “code”,
“marks” : “100”,
“list”: [1,2,9]}
a[“key”] # Prints value
a[“list”] # Prints [1,2,9] '''
Consider the following dictionary,
a = {“name”: “Raushan”,
“from”: “India”,
“marks”: [92,98,96]}
More methods are available on docs.python.org
Set is a collection of non-repetitive elements.
S= Set() # No repetition allowed!
S.add(1)
S.add(2)
# or Set = {1,2}
If you are a programming beginner without much knowledge of mathematical operations on sets, you can simply look at sets in python as data types containing unique values.
Consider the following set:
S = {1,8,2,3}
S = Set()
S.add(20)
S.add(20.0)
S.add(“20”)
What will be the length of S after the above operations?
S = {8, 7, 12, “Raushan”, [1, 2]}
Sometimes we want to play pubg on our phone if the day is Sunday.
Sometimes we order Ice-cream online if the day is sunny.
Sometimes we go hiking if our parents allow.
All these are decisions that depend on the condition being met.
In python programming too, we must be able to execute instructions on a condition(s) being met. This is what conditions are for!
If else and elif statements are a multiway decision taken by our program due to certain conditions in our code.
Syntax:
'''
if (condition1): // if condition 1 is true
print(“yes”)
elif (condition2): // if condition 2 is true
print(“No”)
else: // otherwise
print(“May be”)
'''
Code example:
a = 22
if (a>9):
print(“Greater”)
else:
print(“lesser”)
Quick Quiz: Write a program to print yes when the age entered by the user is greater than or equal to 18.
Relational operators are used to evaluate conditions inside if statements. Some examples of relational operators are:
= = -> equals
>= -> greater than/equal to
<=, etc.
In python, logical operators operate on conditional statements. Example:
and -> true if both operands are true else false
or -> true if at least one operand is true else false
not -> inverts true to false and false to true
elif in python means [else if]. If statement can be chained together with a lot of these elif statements followed by an else statement.
'''
if (condition1):
#code
elif (condition 2):
#code
elif (condition 2):
#code
….
else:
#code '''
Important Notes:
“make a lot of money”, “buy now”, “subscribe this”, “click this”. Write a program to detect these spams.
90-100 | Ex |
80-90 | A |
70-80 | B |
60-70 | C |
50-60 | D |
<50 | F |