0% found this document useful (0 votes)
21 views6 pages

Lec 1 Eg

The document provides a comprehensive overview of basic Python programming concepts, including variables, loops (for and while), lists, sets, and dictionaries. It includes examples of how to manipulate these data structures, such as adding, removing, and accessing elements. Additionally, it covers the characteristics of each data type and demonstrates their usage in Python.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Lec 1 Eg

The document provides a comprehensive overview of basic Python programming concepts, including variables, loops (for and while), lists, sets, and dictionaries. It includes examples of how to manipulate these data structures, such as adding, removing, and accessing elements. Additionally, it covers the characteristics of each data type and demonstrates their usage in Python.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BASIC PYTHON CODE

age = input("How old are you? ")


print ("Your age is", age)
age= 45
print ("You have", 65-age, "years until retirement")

LOGIC EXPRSSION & FOR LOOP


num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
else:
print("Negative number")

WHILE LOOP
Print i as long as i is less than 6:
i = 1
while i < 6:
print(i)
i += 1
FOR LOOP
# iterate from i = 0 to i = 3
for i in range(0, 4):
print(i)

FOR LOOP
language = 'Python'
# iterate over each character in language
for x in language:
print(x)
LIST
thislist = ["apple", "banana", "cherry"]
print(thislist)

Lists allow duplicate values:


thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

Return the third, fourth, and fifth item:


Thislist=["apple", "banana", "cherry", "orange", "kiwi", "melon", "
mango"]
print(thislist[2:5])

Check if "apple" is present in the list:

thislist = ["apple", "banana", "cherry"]


if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")

Change the second item:


thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)

Change the values "banana" and "cherry" with the values


"blackcurrant" and "watermelon":
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)

To add an item to the end of the list, use the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Insert an item as the second position:

thislist = ["apple", "banana", "cherry"]


thislist.insert(1, "orange")
print(thislist)

Remove "banana":

thislist = ["apple", "banana", "cherry"]


thislist.remove("banana")
print(thislist)
Try it Yourself »

Remove the first occurrence of "banana":

thislist = ["apple", "banana", "cherry", "banana", "kiwi"]


thislist.remove("banana")
print(thislist)

Remove the second item:

thislist = ["apple", "banana", "cherry"]


thislist.pop(1)
print(thislist)

The del keyword also removes the specified index:

Remove the first item:

thislist = ["apple", "banana", "cherry"]


del thislist[0]
print(thislist)

The del keyword can also delete the list completely.

Delete the entire list:


thislist = ["apple", "banana", "cherry"]
del thislist

Clear the List


The clear() method empties the list.

The list still remains, but it has no content.

Clear the list content:

thislist = ["apple", "banana", "cherry"]


thislist.clear()
print(thislist)
Make a copy of a list with the copy() method:

thislist = ["apple", "banana", "cherry"]


mylist = thislist.copy()
print(mylist)
Set
thisset = {"apple", "banana", "cherry"}
print(thisset)

Duplicate values will be ignored:

thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)

Loop through the set, and print the values:

thisset = {"apple", "banana", "cherry"}

for x in thisset:
print(x)

Check if "banana" is present in the set:

thisset = {"apple", "banana", "cherry"}

print("banana" in thisset)

Check if "banana" is NOT present in the set:

thisset = {"apple", "banana", "cherry"}

print("banana" not in thisset)

Change Items
Once a set is created, you cannot change its items, but you can add new item

Create and print a dictionary:


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

Dictionary Items
Dictionary items are ordered, changeable, and do not allow duplicates.
Dictionary items are presented in key:value pairs, and can be referred to by
using the key name.

Example
Print the "brand" value of the dictionary:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])

Duplicate values will overwrite existing values:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)

Print the number of items in the dictionary:

print(len(thisdict))
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}

You might also like