Python Basics DAY 1 & DAY 2
Python Basics DAY 1 & DAY 2
Step-1 Start
Step-6 Stop
Flowchart
• A flowchart is the graphical or pictorial
representation of an algorithm with the help
of different symbols, shapes, and arrows to
demonstrate a process or a program.
START
Step-1 Start
Step-6 Stop
STOP
Exercises:
1) Algorithm & Flowchart to find the sum of two numbers
C=A+B
Introduction to Python
Why Python ?
• Python is Easy to Use, Simple and Fast to Develop
• It will run on your desktop and is available for Windows, macOS and Linux.
• Built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem
of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and
runtimes (such as .NET and Unity).
Step 2: Installing Visual StudioCode
Step 3: Installing PythonExtension
Step 4: Open a New File andTest
Step 4: Open a New File andTest
Step 4: Open a New File andTest
Python Fundamentals
print("Hello World")
print(sys.version)
Test with commandline
Python Fundamentals
print("Hello World")
print(sys.version)
Python Fundamentals
COMMENTS
Python Comments
• Single Line Comments Start with a pound sign.
VARIABLES
Using Variables in Python
• Variable names can contain only letters, numbers, and
underscores.
print (my_money)
print (my_bal)
print (my_name)
Using Variables in Python : Assignment
x = x + 2
we can also write
x += 2
x = x - 2
we can also write
x -= 2
Variables in Python : Exercise
Create a simple program to store various
details of a student and print them.
1) Name
2) Roll No
3) Semester
4) Department
5) Phone no
6) Email
7) Address
Python Fundamentals
1. Numbers
2. Strings
3. List
4. Tuple
5. Dictionary
Python Fundamentals
NUMBERS
Python NumbersOverview
• Number data types store numeric values.
• It is created when we assign a value to them.
• Using del, we can remove the reference to a variable
A = 10
B = 20
print(A)
del A,B
print(A)
Python Numerical Types
• Python supports three numerical types
Int_eg = 100
float_eg = 3.14
comp_eg = 3.14j
Python Numerical TypesConversion
• Python converts an expression containing mixed types to a
common type for evaluation.
print(int(float_eg))
print(float(int_eg))
Print(str(int_eg))
Python Basic Arithmetic Operators
x = 7, y = 2
Addition: x + y = 9
Subtraction: x - y = 5
Multiplication: x*y = 14
Division: x/y = 3.5
(A+B)2 = A2 + 2*A*B + B2
Python Fundamentals
STRINGS
Strings
• A string is a series of characters.
• We can use single or double quotes around your strings like this
first_name = "Praveena"
last_name = "akhil"
print(my_name)
print(f"Hello, {my_name.title()}!")
Strings – Adding Tab and New Line
• To add a tab to your text, use the characters \t
• To add a newline, use the characters \n
print("Apples\tOranges");
print("Apples\nOranges");
Strings – Removing white space
• Remove white space from right end using the rstrip() method
• Remove white space from left end using the lstrip() method
• Remove white space from both ends the strip() method
print (greet)
print (greet[0]) # first char
print (greet[2:5]) # chars from 3rd to 5th
print (greet[2:]) # from 3rd character
print (greet * 2) # Prints two times
print (greet + "WORLD") # concatenated string
Strings – Formatting using %operator
Strings can also be formatted using the % operator. This gives us
control over how you want your string to be displayed and stored.
({2:4.2f}, we are referring to the parameter in position 2, which is a float and we want it to be
formatted with 2 decimal places and a total length of 4
Strings – Formatting using format() method
Example without any formatting:
make = 'Dell'
dollarRate = 70.256
myText = 'The amount for this {} computer is {}
USD and the exchange rate is {} USD to 1 INR'
.format(make,1299, dollarRate)
print (myText)
Strings – Formatting using format() method
Example without any formatting:
print (myText)
More String Methods –endswith()
endswith method: Return True if the string ends with the specified suffix
endswith (suffix, [start, [end]]) (suffix can also be a tuple of suffixes )
'Superman'.endswith('man') #true
'hello1234'.isalnum()
True
'h e l l o 1 2 3 4'.isalnum()
False
'hello'.isalnum()
=> True
More String Methods – isalpha(),isdigit()
'hello'.isalpha()
=> True
'hello1234'.isalpha()
=> False
'1234'.isalpha()
=> False
'1234'.isdigit()
=> True
More String Methods – islower(), istitle(),isupper()
'hello'.islower()
True
'Hello World'.istitle()
True
'HELLO'.isupper()
=> True
More String Methods – join()
parameter provided is joined by a separator.
separator = '-'
myTuple = ('h', 'e', 'l' ', 'l' ', '0')
separator.join(myTuple)
=> 'h-e-l-l-o'
More String Methods – replace(),split()
# Replace all occurences
'Hello world'.replace('o', 'i')