Python Fundamental
References:
https://www.javatpoint.com/python-tutorial
https://cheatography.com/nouha-thabet/cheat-sheets/python-operators-and-booleans/
Presented by Tr. Han 1
Python Syntax
Reference from internet source
Presented by Tr. Han 2
Python Variables
•The variable's first character must be an underscore or alphabet (_).
•Every one of the characters with the exception of the main person might be a
letter set of lower-case(a-z), capitalized (A-Z), highlight, or digit (0-9).
•White space and special characters (!, @, #, %, etc.) are not allowed in the
identifier name. ^, &, *).
•Identifier name should not be like any watchword characterized in the language.
•Names of identifiers are case-sensitive; for instance, my name, and MyName
isn't something very similar.
•Examples of valid identifiers: a123, _n, n_9, etc.
•Examples of invalid identifiers: 1a, n%4, n 9, etc.
Presented by Tr. Han 3
Declaring Variable and Assigning Values
The equal (=) operator is utilized to assign
worth to a variable.
Presented by Tr. Han 4
Presented by Tr. Han 5
Variable Names
name = "A"
Name = "B"
naMe = "C"
NAME = "D"
n_a_m_e = "E"
_name = "F"
name_ = "G"
_name_ = "H"
na56me = "I"
print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name,
Output: name_,_name, na56me)
Devansh
20 Output:
80.5
ABCDEDEFGFI
Presented by Tr. Han 6
The multi-word keywords can be created by the following method.
Camel Case - In the camel case, each word or abbreviation in the middle of begins
with a capital letter. There is no intervention of whitespace. For example -
nameOfStudent, valueOfVaraible, etc.
Pascal Case - It is the same as the Camel Case, but here the first word is also
capital. For example - NameOfStudent, etc.
Snake Case - In the snake case, Words are separated by the underscore. For
example - name_of_student, etc.
Presented by Tr. Han 7
Multiple Assignment
Example code:
Example code:
x=y=z=50
print(x) a,b,c=5,10,15
print(y) print a
print(z) print b
print c
Output:
Output:
50
50 5
50 10
15
Presented by Tr. Han 8
Data Types in Python
a=10
b="Hi Python"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
Presented by Tr. Han 9
Standard Data Type
Presented by Tr. Han 10
Introducing Python Keywords
Presented by Tr. Han 11
Python Literals
String literals:
Single-line String
Multi-line String
Presented by Tr. Han 12
Numeric literals:
Presented by Tr. Han 13
Boolean literals:
Presented by Tr. Han 14
Special literals
Presented by Tr. Han 15
List
Presented by Tr. Han 16
Tuple
❑ Python tuple is a collection of different data-type. It is
immutable which means it cannot be modified after
creation.
❑ It is enclosed by the parentheses () and each element
is separated by the comma(,).
Presented by Tr. Han 17
Set ❑ Python set is the collection of the unordered
dataset.
❑ It is enclosed by the {} and each element is
separated by the comma(,).
Presented by Tr. Han 18
Dictionary ❑ Python dictionary stores the data in the key-value
pair.
❑ It is enclosed by curly-braces {} and each pair is
separated by the commas(,).
Presented by Tr. Han 19
Presented by Tr. Han 20
Presented by Tr. Han 21
Presented by Tr. Han 22
Presented by Tr. Han 23
Presented by Tr. Han 24
Presented by Tr. Han 25
Presented by Tr. Han 26
Presented by Tr. Han 27
Python Comments
Presented by Tr. Han 28
Conditional Statement
Presented by Tr. Han 29
Presented by Tr. Han 30
Presented by Tr. Han 31
Presented by Tr. Han 32
Loop
Presented by Tr. Han 33
Presented by Tr. Han 34
User input in Python
For example,
name = input("Give me your name: ")
print("Your name is " + name)
What this will print in the terminal (or the shell, whatever you are running Python in) will be:
>>> Give me your name: Michele
Your name is Michele
Presented by Tr. Han 35
Manipulating strings (a few ways)
age = input("Enter your age: ")
age = int(age)
(or, if you want to be more compact with your code)
age = int(input("Enter your age: "))
print("Were" + "wolf")
print("Door" + "man")
print("4" + "chan")
print(str(4) + "chan")
The same works for multiplication:
print(4 * "test")
Presented by Tr. Han 36
Sample Exercises
Presented by Tr. Han 37
1. Python program to check whether the given number is even or
not.
number = input("Enter a number: ")
x = int(number)%2
if x == 0:
print("The number is Even.")
else:
print("The number is Odd.")
Output:
Enter a number: 7
The number is Odd.
Enter a number: 6
The number is Even.
Presented by Tr. Han 38
2. Python program to convert the temperature in degree
centigrade to Fahrenheit
c = input("Enter temperature in Centigrade: ")
f = (9*(int(c))/5)+32
print("Temperature in Fahrenheit is: ", f)
Output:
Enter temperature in Centigrade: 30
Temperature in Fahrenheit is: 86.0
Presented by Tr. Han 39
3. Python program to find the area of a triangle whose
sides are given
import math
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))
s = (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ", area)
Output:
Enter the length of side a: 4.0
Enter the length of side b: 3.0
Enter the length of side c: 6.0
Area of the triangle is: 5.332682251925386
Presented by Tr. Han 40
4. Python program to find out the average of a set of integers
count = int(input("Enter the count of numbers: "))
i=0
sum = 0
for i in range(count):
x = int(input("Enter an integer: "))
sum = sum + x
avg = sum/count
print("The average is: ", avg)
Output:
Enter the count of numbers: 5
Enter an integer: 3
Enter an integer: 6
Enter an integer: 8
Enter an integer: 5
Enter an integer: 7
The average is: 5.8
Presented by Tr. Han 41
5. Python program to find the product of a set of real
numbers
i=0
product = 1
count = int(input("Enter the number of real numbers: "))
for i in range(count):
x = float(input("Enter a real number: "))
product = product * x
print("The product of the numbers is: ", product)
Output:
Enter the number of real numbers: 4
Enter a real number: 3.2
Enter a real number: 2.9
Enter a real number: 7.4
Enter a real number: 5.5
The product of the numbers is: 377.69599999999997
Presented by Tr. Han 42
Presented by Tr. Han 43
Write your notes..
Presented by Tr. Han 44
Presented by Tr. Han 45
Presented by Tr. Han 46