4. A Program is a set of instructions that a computer follows to perform a particular
task.
Program
Step 1
Plug the TV wire to
the electric switch
board.
Step 2
Turn your Tv on.
Step 3
Press the power
button on your TV
remote.
Step 4
Switch between the
channels by directly
entering the numbers
from your remote.
How to turn on a tv with remote
5. Programming is the process of writing your ideas into a program using a computer
language to perform a task.
Programming
Programming languages
6. â It was created in 1991 by Guido van Rossum.
â Python is an interpreted, interactive, object-oriented programming language.
â Python is a high-level general-purpose programming language that can be
applied to many diïŹerent classes of problems.
â Python is portable: it runs on many Unix variants, on the Mac, and on Windows
2000 and later
â Easy to learn and understand.
â Simple syntax and Flexible.
What is Python?
7. Where it is used?
Web and Internet
Development
ScientiïŹc & Numeric Games Desktop GUI
Software
Development
Business
Applications
Cloud & Data Center
IoT, Machine
Learning & AI
13. Comments
# Single Line Comment
# multi
# line
# Comment
""â
Docstring is short for documentation string.
It is a string that occurs as the ïŹrst statement in a module, function, class, or method deïŹnition.
We must write what a function/class does in the docstring.
"""
17. Variables
>>> apples_in_box = 10
5
Variable is the container that stores some value.
>>> apples_sold = 5
>>> apples_balance = apples_in_box - apples_sold
5
10
>>> fruit_box = 100
variable name Value to be stored
18. Naming Variables
Name your variables meaningful and As long as youâre not breaking these rules,
you can name variables anything you want.
no spaces = 0 X No spaces in the name
3eggs, $price X No digits or special characters in front
19. Constants
Constants are used to store a value that can be used later in your program. The
value of the constant remains same and they do not change.
>>> PI = 3.14
constant name Value to store
>>> GRAVITY = 9.8
20. Input and Output
>>> input([prompt])
Function to accept
value from users
via cli
Value to be
entered by user
>>> print([output])
Function to output
value to users
Value printed on
the user screen
22. Strings
A string in Python is a sequence of characters. For Python to recognize a
sequence of characters, like hello, as a string, it must be enclosed in quotes to
delimit the string.
>>> "Hello"
Note:
A string can have any number of characters in it, including 0. The empty string is '' (two quote characters with nothing
between them). Many beginners forget that having no characters is legal. It can be useful.
>>> 'Hello'
23. Concatenation
The plus operation with strings means concatenate the strings. Python looks at
the type of operands before deciding what operation is associated with the +.
>>> "Firstname" + "lastname" >>> 'Hello'
Firstnamelastname
>>> 3 * 'Smart' + 'Phone'
SmartSmartSmartPhone
>>> 7 + "star"
?
24. Note:
The line structure is preserved in a multi-line string. As you can see, this also allows you to embed
both single and double quote characters!
Triple Quoted String Literals
Strings delimited by one quote character are required to be within a single line.
For example: âhelloâ.It is sometimes convenient to have a multi-line string, which
can be delimited with triple quotes, '''.
25. Note:
The line structure is preserved in a multi-line string. As you can see, this also allows you to embed
both single and double quote characters!
Escape Codes
Escape codes are embedded inside string literals and start with a backslash
character . They are used to embed characters that are either unprintable or
have a special syntactic meaning to Python that you want to suppress.
26. String Methods
Python has quite a few methods that string objects can call to perform frequency
occurring task (related to string). For example, if you want to capitalize the ïŹrst
letter of a string, you can use capitalize() method
capitalize() Converts first character to capital letter
count() Counts the occurrences of a substring
upper() Returns uppercased string
lower() Returns lowercased string
len() Returns length of a string
split() Separates the words in sentences.
28. if , elif and else
while
for
break
Control
Structures
Module 4
29. if, elif & else
if condition:
statements
else:
statement
if condition:
statements
elif condition:
statements
else:
statements
a = 0
if a == 0:
print('A is equal to 0')
else:
print('A is not equal to 0')
a = 2
if a == 0:
print('A is equal to 0')
elif a == 1:
print('A is equal to 1')
else:
print('A is not equal to 0 or 1')
The
ifâŠelifâŠelse
statement is
used for
decision
making.
Syntax Syntax
Run Run
30. Login
uname = 'admin'
pword = 'admin'
# user to enter their usernname & password
usernname = input('username: ')
password = input('password: ')
if usernname == uname and password == pword:
print('Login successful')
else:
print('Sorry! Invalid username and password')
31. while
Run the statements until a given condition is true.
while condition:
statement
Syntax
movie = 'Spider Man'
while movie == 'Spider Man':
print('Yes! I am Spider Man')
break
Run
32. for
Loops through elements in sequence & store in a variable
that can be used later in the program.
for variable in sequence:
statement
Syntax
33. for
# print 0-9 numbers using for loop
for number in range(10):
print(number)
Run
# print all the elements in fruits list
fruits = ['apple','banana','orange']
for fruit in fruits:
print(fruit)
Run
34. for - nested
countries = ['India','US','China','Europe']
fruits = ['apple','banana','orange']
for country in countries:
for fruit in fruits:
print(country,':', fruit)
print('-----------------')
Run
35. break
a = 0
while a==0:
print('hello')
Run
a = 0
while a==0:
print('hello')
break
Run