Python Introduction
PYTHON
PROGRAMING
Python supports
multiple programming paradigms, including object-oriented, procedural,
and functional programming. It provides a rich standard library and a
large ecosystem of third-party packages, which helps developers to build
applications faster and more efficiently.
Python is platform-independent, meaning Python programs can run on
Windows, Linux, and macOS without modification. Due to its flexibility
and powerful features, Python is widely used in fields such as:
·
Web Development
·
Data Science & Data Analytics
·
Artificial Intelligence &
Machine Learning
·
Automation & Scripting
·
Desktop Application Development
·
Game Development
·
Internet of Things (IoT)
·
Cyber Security
HOW TO COMMENT IN
PYTHON
1.
SINGLE LINE COMMENT ( # )
2.
MULTI LINE COMMENT ( 3 INVERTED COMMA
CONTINUE)
“””
MESSAGE
“””
Hello World
print("Hello,
World!")
Variables and Data
Types
name = "Nilesh"age = 25salary = 35000.50is_student = True print(name)print(age)print(salary)print(is_student)
Taking User Input
String Input
name = input("Enter your name:
")
print("Welcome,", name)
Number Input
num = int(input("Enter a
number: "))
decimal
Input
num = float(input("Enter a
decimal number: "))
print("You entered:",
num)
Program Management
IF ELSE , ELSE
IF , SWITCH
IF ELSE
marks = 35
if marks >= 40:
print("Pass")
else:
print("Fail")
ELSE IF
percentage = 30
if percentage >= 60:
print("Grade A")
elif percentage >=
50 and
percentage < 60:
print("Grade B")
elif percentage >=
40 and
percentage < 50:
print("Grade C")
else:
print("fail")
SWITCH CASE
day
= 3
match
day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
case _:
print("Invalid day")
while / do while / for loop
while :
i = 1
while i <= 5:
print(i)
i=i+1
do while
i = 1
while True:
print(i)
i
=i+1
if
i > 10:
break
For loop
for i in range(1, 11):
print(i)
List
Lists
are used to store multiple items in a single variable.
Lists
are one of 4 built-in data types in Python used to store collections of data,
the other 3 are Tuple, Set, and Dictionary, all with different
qualities and usage.
Lists
are created using square brackets:
thislist = ["apple", "banana", "cherry"]
print(thislist)
Access
Items
List
items are indexed and you can access them by referring to the index number:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Change Item
Value
To
change the value of a specific item, refer to the index number:
thislist
= ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Append
Items
To
add an item to the end of the list, use the append() method:
thislist
= ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Remove
Specified Item
The remove() method removes
the specified item.
thislist
= ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Loop
Through a List
You
can loop through the list items by using a for loop:
thislist
= ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Sort List
Alphanumerically
List
objects have a sort() method that will
sort the list alphanumerically, ascending, by default:
thislist
= ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
Use the copy()
method
You
can use the built-in List method copy() to copy a list.
thislist
= ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
Join Two
Lists
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
clear
list data
fruits = ["apple",
"banana", "cherry"]
fruits.clear()
print(fruits)
|
Method |
Description |
|
Adds an element at the end of the list |
|
|
Removes all the elements from the list |
|
|
Returns a copy of the list |
|
|
Returns the number of elements with the specified value |
|
|
Add the elements of a list (or any iterable), to the end of the
current list |
|
|
Returns the index of the first element with the specified value |
|
|
Adds an element at the specified position |
|
|
Removes the element at the specified position |
|
|
Removes the item with the specified value |
|
|
Reverses the order of the list |
|
|
Sorts the list |
TupleS
Key
Differences Explained
- List is mutable
→ You can add, remove, or modify elements.
- Tuple is immutable
→ Once created, you cannot change it.
- Tuple is more memory efficient than list.
- Tuple is used for fixed data like coordinates, days of week, etc.
Tuple
Tuples
are used to store multiple items in a single variable.
Tuple
is one of 4 built-in data types in Python used to store collections of data,
the other 3 are List, Set, and Dictionary, all with different
qualities and usage.
A
tuple is a collection which is ordered and unchangeable.
Tuples
are written with round brackets.
[1] Access Tuples
thistuple
= ("apple", "banana", "cherry")
print(thistuple[1])
[2] change tuples data
using list can not change direct
x
= ("apple", "banana", "cherry")
y
= list(x)
y[1]
= "kiwi"
x
= tuple(y)
print(x)
[3] Loop using Tuples
thistuple
= ("apple", "banana", "cherry")
for
x in thistuple:
print(x)
Python SET
Comments
Post a Comment