0% found this document useful (0 votes)
31 views

Python Programs

The document contains Python code examples demonstrating various fundamental programming concepts including: 1) Defining and printing variables of different data types like strings and integers. 2) Performing string operations like concatenation, length, counting characters, capitalization etc. 3) Taking input from the user, performing mathematical operations and using math functions. 4) Slicing and indexing strings to extract substrings. 5) Using conditional statements like if-elif-else and loops to iterate through ranges. So in summary, the document serves as a tutorial covering basic Python programming techniques.

Uploaded by

Khan Shinwari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Python Programs

The document contains Python code examples demonstrating various fundamental programming concepts including: 1) Defining and printing variables of different data types like strings and integers. 2) Performing string operations like concatenation, length, counting characters, capitalization etc. 3) Taking input from the user, performing mathematical operations and using math functions. 4) Slicing and indexing strings to extract substrings. 5) Using conditional statements like if-elif-else and loops to iterate through ranges. So in summary, the document serves as a tutorial covering basic Python programming techniques.

Uploaded by

Khan Shinwari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

first program:

name = "bahadar khan shinwari haji" #assigning string to variable name


age = 58 #assigning value to variable age
print(name) #desplaying data stored in variable name
print(type(name)) #checking type of data stroed in variable name
print ("my grandfather name is: " + name) #concatination of string with variable
print("my grandfather age is: "+ str(age)) #concatination of string with int type
variable
____________

multiple assignment

name,age,height = "bahadar khan shinwari haji",58,5.23


print (name + str(age)+ str(height))
________

WITH TEXT (STRING)

name,age,height = "bahadar khan shinwari haji",58,5.23


print (name + str(age)+ str(height))
print(name)
print(type(name))
print ("my grandfather name is: " + name)
print("my grandfather age is: "+ str(age))
print(len(name))
print(name.count("a"))
print(name.capitalize())
print(name.upper())
print(name.lower())
print(name.isalpha())
print(name.isdigit())
print(name.replace("b","B"))
print(name*2)

________________

SOME BAISC OPERATION STRING

name,age,height = "bahadar khan shinwari haji",58,5.23


print (name + str(age)+ str(height))
print(name)
print(type(name))
print ("my grandfather name is: " + name)
print("my grandfather age is: "+ str(age))
print(len(name))
print(name.count("a"))
print(name.capitalize())
print(name.upper())
print(name.lower())
print(name.isalpha())
print(name.isdigit())
print(name.replace("b","B"))
print(name*2)
print(name.find("a"))

x=12
y=12.4
z="66"
TAKING VALUES FROM USER

print(str(x))
print(float(z))
print(int(y))
print(type(x))
i = int(input("enter first value: "))
j = int(input("enter second value: "))
l = i+j
print("your addition is: " +str(l))

_______________

SOME MATH FUNCTION

import math
x=12
y=12.4
print(math.sqrt(x))
print(abs(-34))
print(pow(x,3))
print(math.ceil(y))
print(math.floor(y))
print(round(y))
print(max(x,y))
print(min(x,y))

________________

slicing() and indexing[]

#INDEXING METHOD

first_name = name[0:7]
print("first name is: "+first_name)
middle_name = name[8:22]
print("middle name is: "+middle_name)
last_name = name[22:27]
print("last name is: "+last_name)
reversed_name = name[::-1]
print("reversed of full name is: "+reversed_name)

#SLICING METHOD

father_name = "taqseer ahmad shinwari"


spe_name = slice(7,-4)
print(name[spe_name])
print(father_name[spe_name])
_________________________
2nd DAY PROGRAM

_:IF ELIF AND ELSE STATEMENT:_

marks=int(input("enter your marks"))


if marks<=100 and marks>=90:
print("you got A+ grad")
elif marks <= 89 and marks >= 80:
print("you got A grad")
elif marks <= 79 and marks >= 70:
print("you got B grad")
elif marks <= 69 and marks >= 60:
print("you got C grad")
elif marks<=59 and marks>=50:
print("you got D grad")
elif marks >100:
print("marks above from 100! enter marks between 0 and 100?")
else:
print("you are fail man!")
___________________________
Reverse in for loop

for i in range(100,50-1,-2):
print(i)

___________________________
time library

# for i in range(100,50-1,-2):
# print(i)
import time
for i in range(1,100,3):
print(i)
time.sleep(i) #it will wait for 3 seconds after each iteration

print("Bahadar khan shinwari Haji")


_________________________
PROGRAM TO PRINT RECTANGLE USING NESTED LOOP

rows = int(input("enter numbers of rows: "))


columns = int(input("enter numbers of columns: "))
symble = input("enter symble to display: ")

for i in range(rows):
for j in range(columns):
print(symble, end="")
print()

You might also like