Python Basic
Python Basic
INTRODUCTION TO PYTHON:
Python is Interpreted
Python is Interactive
Python is Object-Oriented
As mentioned before, Python is one of the most widely used language over the web. I'm going to list
few of them here:
Easy-to-learn
Easy-to-read
Easy-to-maintain
Portable
APPLICATIONS OF PYTHON
CHARACTERISTICS OF PYTHON
Program 1:
• Print(“"Hello, Python!")
Program 2:
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
PYTHON DATA TYPES
• Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric int, float, complex
Types:
Mapping dict
Type:
z = -3255522 z = -35.59
print(type(x))
print(type(y))
print(type(x)) print(type(z))
print(type(x))
print(type(y))
print(type(y))
print(type(z))
print(type(z))
STRINGS
Single Line:
a = "Hello"
print(a)
Multiline Strings:
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
• Slicing:
b = "Hello, World!"
print(b[2:5])
• Upper Case:
a = "Hello, World!"
print(a.upper())
• Lower Case:
a = "Hello, World!"
print(a.lower())
• String Concatenation:
a = "Hello"
b = "World"
c=a+b
print(c)
• String Replace:
a = "Hello"
a.replace(“H”,”K”)
• Remove Whitespace:
a = " Hello, World! "
print(a.strip())
PYTHON LISTS
•Lists are the most versatile of Python's compound data types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them
is that all the items belonging to a list can be of different data type.
Append Items:
Insert Items:
Extend List:
Del:
Sort Descending:
Copy a List:
mylist = thislist.copy()
print(mylist)
PYTHON TUPLES
•A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples
are enclosed within parentheses.
•The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are
enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. For example
• print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
• print tuple[2:] # Prints elements of the tuple starting from 3rd element
Negative Indexing:
x[1]=“Kiwi”
print(x)
Del:
•Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example − #!/usr/bin/python
• dict = {}
• dict['one'] = "This is one"
• dict[2] = "This is two"
• tinydict = {'name': 'john','code':6734, 'dept': 'sales’}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
Change Values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
Update Dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
Adding Items:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Removing Items
• POP:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
• Del
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
• Clear
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
DATA TYPE (SPECIAL ):
• Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and
Dictionary, all with different qualities and usage.
Create a Set:
Remove Item:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
Discard Item:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
Clear:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
Del:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
Pop:
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)