0% found this document useful (0 votes)
8 views7 pages

"Hello World": Print

This document contains examples of basic Python concepts like variables, data types, operators, control flow (if/else statements, loops), functions, and data structures (lists, tuples, sets). It shows how to define and use integers, floats, strings, Booleans, perform calculations and comparisons, iterate through lists and ranges, check conditions, define functions, and perform set operations. The examples demonstrate fundamental Python syntax and how to work with common data types.

Uploaded by

Muhammad Talal
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)
8 views7 pages

"Hello World": Print

This document contains examples of basic Python concepts like variables, data types, operators, control flow (if/else statements, loops), functions, and data structures (lists, tuples, sets). It shows how to define and use integers, floats, strings, Booleans, perform calculations and comparisons, iterate through lists and ranges, check conditions, define functions, and perform set operations. The examples demonstrate fundamental Python syntax and how to work with common data types.

Uploaded by

Muhammad Talal
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/ 7

# -*- coding: utf-8 -*-

"""F2021376101.ipynb

Automatically generated by Colaboratory.

Original file is located at


https://colab.research.google.com/drive/1lClHjWbHUJoqlkKO00pyL_-kbM_y-T7L

# F2021376101

#Basic Examples using Pyhton

#Python syntax
"""

# Example 1:
print("Hello World")

# Example 2:
x = 10
y = 5

# Example 3:
if x > y:
print("x is greater than y")

# Example 4:
for i in range(5):
print(i)

# Example 5:
name = "TALAL"
print(name)

"""#Python Comments

"""

# Example 1: print("Putting '#' symbol before the words will make the line a
comment") # Example 2: for multiple lines comments we have to use '#' before every
line
# otherwise it will not work
"""
Example 3: We can aslo use triple quotation rather than using '#' before every line

"""
print("Hello, World!")

"""#Python Variables"""

# Example 1: Integer variable


age = 25
print(age)
# Example 2: String variable
name = "Muhammad Talal"
print(name)
# Example 3: Float
variable value = 100.444
print(value)
# Example 4: Output variable
a = "I"
b = "am"
c = "Talal"
print(a, b, c)

# Example 5: Global variable


x = "Student"

def myfunc():
x = "Teacher"
print("I am " + x)

myfunc()

print("I am " + x)
"""#Python Data Types"""

# Example 1: Integer type


x = 5
print(type(x))
# Example 2: string type
y = "Talal"
print(type(y))
# Example 3: Float type
z = 100.11
print(type(z))
# Example 4: Boolean type
abc = True
print(type(abc))
# Example 5: Bytes type
aa = b"Hello"
print(type(aa))

"""#Python Numbers"""

# Example 1: Integer
x = 10
print(x)
# Example 2: Float
y = 5.7
print(y)
# Example 3: Complex
z = 3 + 4j
print(z)
# Example 4: Binary
binary_num =
0b1010
print(binary_num)
# Example 5: Hexadecimal
hex_num = 0x1F
print(hex_num)

"""#Python Casting"""

# Example 1
x = int(1)
print(x)
# Example 2
y =
int(2.8)
print(y)
# Example 3
z =
int("3")
print(z)
# Example 4
a = float(1)
print(a)
# Example 5
abc = bool(10)
print(abc)

"""#Python Strings"""

# Example 1: Single-line string


x = "Hello, World!"
print(x)
# Example 2: Multi-line string
multiline_str = """This is
a multi-line
string."""
print(multiline_str)
# Example 3: String concatenation
name = "Talal"
y = "Hello, " + name
print(y)
# Example 4: String formatting
age = 25
format = f"My age is {age} years."
print(format)
# Example 5: Accessing characters
abc = x[0]
print(abc)
"""#Python Boolean"""

# Example 1: True boolean


is_true = True
print(is_true)
# Example 2: False boolean
is_false = False
print(is_false)
# Example 3: Boolean condition
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than
a") # Example 4: Boolean in a
function def myFunction() :
return True

if myFunction():
print("YES!")
else:
print("NO!")
# Example 5: Check if an object is an integer or not:
x = 200
print(isinstance(x, int))

"""#Python Operators"""

# Example 1: Arithmetic operators


sum = 5 + 3
sub = 10 - 4
mul = 2 * 6
div = 8 / 2
mod = 10 % 3

# Example 2: Comparison operators


is_equal = 5 == 5
not_equal = 10 != 5
print(is_equal)
print(not_equal)

# Example 3: Logical operators


x < 5 and x < 10
x < 5 or x < 4
not(x < 5 and x < 10)

# Example 4: Assignment operators


a = 5
a += 3
print(a)

# Example 5: Membership operators


fruits = ["apple", "banana", "orange"]
is_in_list = "apple" in fruits
print(is_in_list)

"""#Python Lists"""

# Example 1: Creating a list


fruits = ["apple", "banana", "orange"]
print(fruits)
# Example 2: Accessing elements
first = fruits[0]
last = fruits[-1]
print(first)
print(last)
# Example 3: Modifying elements
fruits[1] = "grape"
print(fruits)

# Example 4: Adding elements


fruits.append("kiwi")
print(fruits)
# Example 5: Removing elements
removed_fruit = fruits.pop(1)
print(removed_fruit)
print(fruits)

"""#Python Tuples"""

# Example 1: creating a tuple


thistuple = ("apple", "banana", "cherry")
print(thistuple)
# Example 2: Allow Duplicates
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
# Example 3: Tuple length
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
# Example 4: Tuple using one item
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
# Example 5: Tuple can contain different data
types tuple1 = ("abc", 34, True, 40, "male")
print(tuple1)

"""#Python Sets"""

# Example 1: Creating a set


fruits_set = {"apple", "banana", "orange"}
print(fruits_set)
# Example 2: Adding elements
fruits_set.add("kiwi")
print(fruits_set)
# Example 3: Removing elements
fruits_set.remove("banana")
print(fruits_set)
# Example 4: Set operations
all_fruits = fruits_set.union({"grape", "melon"})
common_fruits = fruits_set.intersection({"orange", "kiwi"})
print(all_fruits)
print(common_fruits)
# Example 5: Checking membership
is_in_set = "apple" in fruits_set
print(is_in_set)

"""#Python If...Else"""

# Example 1: Basic if statement


x = 10
if x > 5:
print("x is greater than 5")

# Example 2: If-else statement


y = 3
if y % 2 == 0:
print("y is even")
else:
print("y is odd")

# Example 3: Elif statement


grade = 75
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
else:
print("C")

# Example 4: Nested if statements


num = 7
if num > 0:
if num % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")

# Example 5: Inline if-else (ternary operator)


a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")

"""#Python While Loops"""

# Example 1: Basic while loop


count = 0
while count < 5:
print(count)
count += 1

# Example 2: While loop with break statement


number = 1
while True:
print(number)
if number == 5:
break
number += 1

# Example 3: While loop with continue statement


num = 0
while num < 5:
num += 1
if num == 3:
continue
print(num)

# Example 4: While loop with else statement


index = 0
while index < 3:
print(index)
index += 1
else:
print("Loop finished")

# Example 5: Infinite loop


#while True:

"""#Python For Loops"""

# Example 1: Basic for loop


for i in range(5):
print(i)

# Example 2: Looping through a list


fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)

# Example 3: Range
for x in range(6):
print(x)

# Example 4: Nested for loop


adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)
# Example 5: For loop with else statement
for num in range(5):
print(num)
else:
print("Loop finished")

"""1. Calculate and print the sum, average, minimum, and maximum of the input numbers:"""

n = [1, 2, 3, 4, 5, 6]
sum_res = sum(n)
average_res = sum_res / len(n)
min_res = min(n)
max_res = max(n)

print("Sum:", sum_res)
print("Average:", average_res)
print("Minimum:", min_res)
print("Maximum:", max_res)

"""2. Create two sets, one containing even numbers and the other containing odd

---

numbers from the list:


"""

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = set(num for num in numbers if num % 2 == 0)


odd_numbers = set(num for num in numbers if num % 2 != 0)

print("Even Numbers Set:", even_numbers)


print("Odd Numbers Set:", odd_numbers)

"""3. Check whether a word is a palindrome or not:"""

word = input("Enter a word: ")

if word == word[::-1]:
print("YES")
else:
print("NO")

"""4. Check and print whether set_A is a subset of set_B:"""

set_A = {1, 2, 3}
set_B = {1, 2, 3, 4, 5}

if set_A.issubset(set_B):
print("set_A is a subset of set_B")
else:
print("set_A is not a subset of set_B")

"""5. Find and print the symmetric difference of set_A and set_B:

"""

set_A = {1, 2, 3}
set_B = {3, 4, 5}

symmetric_difference = set_A.symmetric_difference(set_B)

print("Symmetric Difference:", symmetric_difference)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy