Python Notes
Python Notes
Syntax: Similar to Grammar rules in English, Hindi, each programming language has a unique set
of rules.
These rules are called the Syntax of a Programming Language.
Why Python?
Python is an easy to learn, powerful programming language. With Python, it is possible to create
programs with minimal amount of code.
Applications of Python?
Career Opportunities: Python developers have plenty of opportunities across the world
DevOps Engineer
Software Developer
Data Analyst
Data Scientist
Machine Learning (ML) Engineer
AI Scientist
Possible mistakes:
print(2+5) o/p: 7
print(1+1.5) 2.5
Subtraction: Subtraction is denoted by - sign. It gives the difference between two numbers.
print(5-2) o/p: 3
print(1.5-1.0) 0.5
print(2 * 5) o/p: 10
print(5 * 0.5) 2.5
Variables: Variables are like containers for storing values. Values in the variables can be changed.
Values: Consider that variables are like containers for storing information. In context of
programming, this information is often referred to as value.
Data Type: In programming languages, every value or data has an associated type to it known as
data type. Some commonly used data types
String
Integer
Float
Boolean
This data type determines how the value or data can be used in the program. For example,
mathematical operations can be done on Integer and Float types of data.
Stream of Characters:
Capital Letters (A – Z)
Small Letters (a – z)
Digits (0 – 9)
Special Characters (~ ! @ # $ % ^ . ? ,)
Space
Ex: “Hello World!” , “some@example.comm”, “1234”
Integer: All whole numbers (positive, negative and zero) without any fractional part come under
Integers.
Ex: …, -3, -2, -1, 0, 1, 2, 3,…
Float: Any number with a decimal point.
Ex: 24.3,345.210, -321.86
Boolean: Anything that can take one of two possible values is considered a Boolean.
As per the Python Syntax, True and False are considered as Boolean values. Notice that both start
with a capital letter.
age = 10
Defining a Variable: A variable gets created when you assign a value to it for the first time.
age = 10
print(age) o/p: 10
print("age") age
Note: Variable name enclosed in quotes will print variable rather than the value in it. If you intend
to print value, do not enclose the variable in quotes.
a = 10 * 5
b = 5 * 0.5
b = a + b
o/p:
File "main.py", line 3
b = a + b
^
IndentationError: unexpected indent
Variable Assignment: Values in the variables can be changed.
a = 1
print(a) o/p: 1
a = 2
print(a) o/p: 2
Brackets (B)
Orders (O)
Division (D)
Mutltiplication (M)
Addition (A)
Subtraction (S)
(5 * 2) + (3 * 4)
(10) + (12)
22
Take Input From User: input() allows flexibility to take the input from the user. Reads a line of
input as a string.
String Repetition: * operator is used for repeating strings any number of times as required .
a = "*" * 10
print(a)
o/p: **********
username = input()
length = len(username)
print(length)
i/p: Python
o/p: 6
String Indexing: We can access an individual character in a string using their positions (which start
from 0) . These positions are also called as index.
username = "Python"
first_letter = username[0]
print(first_letter)
o/p: P
Index Error: Attempting to use an index that is too large will result in an error
username = "Python"
print(username[6])
Syntax: variable_name[start_index:end_index]
o/p: Python
Slicing to End: If end index is not specified, slicing stops at the end of the string.
o/p: Python
Slicing from Start: If start index is not specified, slicing starts from the index 0.
o/p: Hi
Checking Data Type: Check the datatype of the variable or value using type().
print(type(10))
print(type(4.2))
print(type("Hi"))
o/p:
<class 'int'>
<class 'float'>
<class 'str'>
Type Conversion: Converting the value of one data type to another data type is called Type
Conversion or Type Casting. We can convert
String to Integer
Integer to Float
Float to String and so on.
a = "5"
a = int(a)
print(type(a))
print(a)
o/p:
<class 'int'>
5
a = "Five"
a = int(a)
print(type(a))
o/p:
ValueError:
invalid literal for int() with base 10: ‘Five’
a = "5.0"
a = int(a)
print(type(a))
o/p:
invalid literal for int() with base 1
a = 2
b = 3
result = a + b
print("Sum: " + str(result))
o/p: Sum: 5
float(): Converts to float data type.
Relational Operators: Relational Operators are used to compare values.Gives True or False as the
result of a comparison.
Operator Name
== Is equal to
!= Is not equal to
Possible Mistakes:
print(3 = 3)
o/p: SyntaxError: expression cannot contain assignment, perhaps you meant
“==?”
print(2 < = 3)
Note: Space between relational operators == , >= , <= , != is not valid in python.
Comparing Integers, Floats and Strings:
print(12 == 12.0) o/p: True
print(12 == 12.1) False
print("ABC" == "ABC") True
print("CBA" != "ABC") True
Case Sensitive:
print("ABC" == "abc") o/p: False
NOTE: Python is case sensitive. It means X(Capital letter) and x(small letter) are not the same in
Python.
Logical operators: The logical operators are used to perform logical operations on Boolean
values Gives True or False as the result. Following are the logical operators
and
or
not
Logical AND Operator: Gives True if both the Booleans are true else, it gives False.
Example:
o/p: True
Logical OR Operator: Gives True if any one of the Booleans is true else, it gives False.
print(False or False)
o/p: False
Example:
o/p: True
Logical NOT Operator: Gives the opposite value of the given Boolean.
print(not(False))
o/p : True
Example:
o/p: False
Block of Code: A Sequence of instructions are called block of code. Python executes code in a
sequence.
Conditional Statement: Conditional statement allows you to execute a block of code only when
Indentation:
Possible Mistakes:
Each statement inside a conditional block should have the same indentation (spacing).
If- Else Syntax: When If – Else conditional statement is used, Else block of code executes if the
condition is False.
a = int(input())
if a > 0:
print("Positive")
else:
print("Not Positive")
print("End")
i/p: 2
o/p: Positive
End
Modulus: To find the remainder, we use modulus operator %
print(6 % 3)
o/p: 0
print(2 ** 3)
o/p: 8
Square root: To calculate square root of a number, we can use exponent operator with 0.5
print(16 ** 0.5)
o/p: 4.0
You can use the exponent operator to calculate the square root of a number by keeping the
exponent as 0.5
print(16 ** 0.5)
o/p: 4.0
Nested Conditional Statements: The conditional block inside another if/else conditional
blockis called as nested conditional block.
a = 2
b = 3
c = 1
is_a_greatest = (a > b) and (a > c)
if is_a_greatest:
print(a)
else:
is_b_greatest = (b > c)
if is_b_greatest:
print(b)
else:
Elif Statement:
print(c)Use the elif statement to have multiple conditional statements between if and
else.The elif statement is optional
o/p:3