0% found this document useful (0 votes)
7 views3 pages

Python Codes

The document contains several Python code snippets demonstrating basic programming concepts such as checking if a number is even or odd, validating a password, calculating a factorial, printing numbers and multiplication tables, and swapping variable values. It also explains the list data type in Python, including creation, accessing, modifying, adding, and removing elements. Each code snippet is accompanied by example outputs to illustrate the results.

Uploaded by

Mehbuba Butt
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)
7 views3 pages

Python Codes

The document contains several Python code snippets demonstrating basic programming concepts such as checking if a number is even or odd, validating a password, calculating a factorial, printing numbers and multiplication tables, and swapping variable values. It also explains the list data type in Python, including creation, accessing, modifying, adding, and removing elements. Each code snippet is accompanied by example outputs to illustrate the results.

Uploaded by

Mehbuba Butt
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

1- #check if the entered number is even or odd.

n = int(input('please enter number'))

if(n%2 == 0):

print('Number is even', n)

else:

print('Number is odd', n)

2- #check if the entered password is correct or not.

password= input('Enter your password')

if(password == 'XYZ'):

print('Entered password is correct', password)

else:

print('Entered password is incorrect', password)

3- #calculate factorial of number

n= int(input('Enter number'))

fact =1
for i in range(1,n+1):

fact = fact * i

print(fact)

4- #print number for 1 to 10

print('number from 1 to 10')

for i in range(1,11):

print(i)

5- #print table of given number

n=int(input('Enter number'))

print('Table of entered number')

for i in range(1,11):

t = n*i
print(f'{n}*{i} = {t}')
6- # Swap the values of two variables.

# Swap two variables

a = 10
b = 20
print("Before swapping:") print("a =", a)
print("b =", b)

Data Types

In Python, a list is a built-in data type used to store a collection of items (elements), which can
be of different types (integers, strings, other lists, etc.). Lists are ordered, mutable (can be
changed), and allow duplicate values.

Method used in List append() and remove()

Syntax of a List

my_list = [item1, item2, item3, ...]

1: Creating and Printing a List


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

print(fruits)

Output:
['apple', 'banana', 'cherry']

2: Accessing Elements
print(fruits[1]) # Accessing second element

Output:
banana

3: Modifying Elements
fruits[0] = "mango"
print(fruits)
Output:
['mango', 'banana', 'cherry']

4: Adding Elements

fruits.append("orange") # Adds to the end print(fruits)


Output:['mango', 'banana', 'cherry', 'orange']

5: Removing Elements

fruits.remove("banana")
print(fruits)
Output:
['mango', 'cherry', 'orange']

You might also like