Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
133 views

Python Training Tutorials

The document provides examples of Python programming concepts like variables, strings, functions, conditional statements, loops, and built-in functions. It demonstrates how to write functions, take user input, perform comparisons and return values. Various examples are given for programming constructs like if-else statements, while loops, and how to break or continue within loops.

Uploaded by

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

Python Training Tutorials

The document provides examples of Python programming concepts like variables, strings, functions, conditional statements, loops, and built-in functions. It demonstrates how to write functions, take user input, perform comparisons and return values. Various examples are given for programming constructs like if-else statements, while loops, and how to break or continue within loops.

Uploaded by

Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Python Definition

How to do print:
What is command in print
String is used in double qoutes”raj”
Simple ()
First Python Program

How to write First Program:


Print(“ Hello World, I am Kamran”)
-Save as
-Press F-5 for execution.
How to use Python as a Calculater
-print command
-()
-there is no string
-operators +,-,*,/
Example of addition:
Print(2+2)
Press F-5
Result: 4
Variable in Python
Variables are location into the memory that can hold the value at
the execution.
Steps for Variable creation:-
-name of the variable
-refer variable
-Assignment statement
-empty box- here store the values
-Value variable
Example:
X=(“Hi, I am Kamran”)
Print(x)
Press F-5
Rules for Creation of Variables
-Use any Alphabets or word i.e. a,b,x,name
-Use word with numbers, i.e. skm452
-Use underscore between two words, i.e
sk_kk
-Start with underscore not number. _raj
Example of Variable
1. speed_of_light = (“299792458”)
print (speed_of_light)
===========
# meter per minute
Light= speed_of_light*60
Print(light)
It is wrong:

----
light= speed_of_light *60
Print (light)
Another way to declare variables in
python
• Hours=(9)
• Hours= (hours+1)
• Hours=(hours*2)
• Print(hours)
String
• It is a combination of words within double
quotes.
• Example
• Print(“my name is”+ kamran)
• Print(“my name is”+”kamran”)
• Print(“my name is”*2)
How to print values from strings

• X=(“abcded”)
• Suppose we want to print c in above string
• The values like this.. 0,1,2,3…..
• Print(x[2])
• c
String in Python Programing
Abc=(“computer)
Print(abc[-6])
========
How to use colon in string.
Abc=(“computer”)
Print(abc[1:])
It will print omputer.
===========
Print(abc[:-2])
It will print comput
Out of range number will give the blank output.
abc=(“computer”)
Print(abc[50:])
It give blank output.
- If we will give out of range value at the end. i.e.
Abc=(“computer”)
print(abc[:40])
Then it will print whole string i.e. computer.
- If we will not give any value then it will print whole string i.e.
Abc=(“computer”)
Print(abc[:])
- You can also print seperated alphabets in strings.
abc=(“computer”)
Print(abc[0:3] +abc[3:])
-if you give the values out of range then it will print whole string.

abc=(“computer”)
Print(abc[0:60] +abc[60:])
Selecting sub sequences in Python
Programming
• [Starting point: ending point]
Abc =(“computer”)
Print(abc[1:3])
Search Engines
• Duck duck go
• Dave Dave Find
• Google
• Bing
• Yahoo
Search Engine Concepts
<html>
<head>
<title> Professionals Lab</title>
</head>
</body>
<b> This is fast growing IT Company in India</b>
<a href="www.facebook"> facebook</a>
</body>
</html>
This simple example of a href tag.
How search engine work
• Search engine first search the a href code in
source code file.
• Then extract the website link
• Within this search double qoutes”” within <a
href = “ www.facebook.com”
• Then index one by one then show the result.
Example of search in python
page=('…<a href="www.facebook">facebook</a>……')
start_link=(page.find('<a href'))
start_qout=(page.find('"',start_link))
end_qout=(page.find('"',start_qout +1))
url=(page[start_qout+1:end_qout])
print(url)
Find Operator in Pythor
• it is used to find position in string using find operator. syntax- print(abc.find(“m”).
Example.
Abc(“computer”)= to find the position of m
Print(abc.find(“m”).
Example for find position in a line.
Abc=(Hi, I am going delhi for interview)
-Print I position in above line.
abc=(“Hi, I am going at Delhi for interview)
Print(abc.find(“p”)
-if you want to print g after that rest of the line.
Abc=(“Hi, I am going to delhi for interview”)
y=(abc.find (“g”)
Print(abc[y:])
* Abc & y are variables.
Exp.
Abc=(“computer”)
Y=(abc.find(“interview”)
Print(y)
-How to find word which is not available in paragraph.
Abc=(“computer”)
Y=(abc.find(“love”)
Print(y)
In this case it will display -1 only.
Abc=(“computer”)
Y=(abc.find(“love”)+5)
Print(y)
In this case it will print 4 only.
How to use number in find operator
I am love you so much, my love.
-how to find values of second same word.
abc=("I love you so much, my love")
a =(abc.find("love"))
b =(abc.find("love",a+1))
print(b)
Or
Print(abc[b:])
It will display after love, rest of the line.
Input Function in Python
• Q .requesting input, program- Hello,-What is your
name- hello nice to meet you.
• Ans.
# requesting input function
#program -Hello- What is your name- Nice meet to
you.
print("Hello")
x= input("What is your name")
print("Hello, Nice to meet you")
Len,int,str() functions in Python
• Len() – it is used to find the character length of string.
- x=(“Computer”)
Print(len(X))
==========
Str() function –it is used to convert values in string
# print- Hello- what is your name
# give the output.
print("Hello")
x= input("what is your name")
print("length of your name is" + str(len(x)))
Int() Function
Int() function- it is used to convert the value
from string to integer.
Example.
x=("25")
y=(int(x))
print(5+y)
Q . Write a program using
str(),len(),int() functions.
# print hello
# ask- Name
#output - length of name
# ask= age
#output= you will be age + 1 in a year
# str,len, int,input, variables.
print("hello")
x = input(" what is your name")
print(x + "length of your name is" + str(len(X))
y = input("what is your age")
print(("you will be") + str(int(y)+1) + (" in a year"))
The Sum of Two numbers:
#sum of two numbers in python
print("wellcome, the sum of two number")
x=input("Enter First number")
x=int(x)
y=input("Enter two number")
y=int(y)
print(("The sum of two number is ") + str(x+y))
Procedure and Function in Python

# Take input or more


Def sum(a,b): # def is a keyword to define function.
Return a+b
Print(sum(5,20))
- Def sum(a,b)
Return a+b
def sum(a,b):
return a+b
x= input("enter first number")
y= input("enter second number")
z=(sum(int(x),int(y)))
print("sum of two number is" + str(z))
# squar of number
def square(a):
return (a*a)
x=input("enter a number")
y=(square(int(x)))
print("square of number is" + str(y))
How to use function in string
# search=" I like to play ball and I am learning to ball.
#input=2
#1=variable
#2=word jisko find krna hai

def position2(search,target):
first=(search.find(target,first+1))
hobby=("i like to paly ball and i am learning ball")
y=(position2(hobby,"guitar"))
print(hobby[y:])
Decision control – if Statement
# decision control- ==(equal to),= (assingnment Operator)
Print(“welcome, this is number guessing, and no is in”)
X= input(“guess the number”) # if statement
X=input(x)
If(x==4):
print(“your guess is right, great, you win”)
If(x!=4):
print(“oops, your guess is wrong”)
If- else statement
# decision control- ==(equal to),= (assingnment
Operator)
Print(“welcome, this is number guessing, and no is
in”)
X= input(“guess the number”) # if statement
X=input(x)
If(x==4):
print(“your guess is right, great, you win”)
else(x!=4):
print(“oops, your guess is wrong”)
Write a program of two number which is greater then

• # which number is greater


• x= input("Enter First Number")
• y= input("Enter Second Number")
• if (x>y):
• print(x+ ("is greater"))
• else:
• print(y + ("is greater"))
Write a program in Python to compare
two values
# how to use function in program.
def greater(a,b):
if(a>b):
return a
else:
return b
x= input("enter first number")
y= input("enter second number")
z= greater(float(x),float(y))
print(str(z) + ("is greater"))
Write a start values with caps A
# write a program of start value caps A
def abc(name):
if(name[0]==("A")):
return True
else:
return False
x= input("Enter First Number")
print(abc(x))
Write a program for Nested Loop, if else if else
# write a program of start value caps A and B
#def abc(name):
# if(name[0]==("A")):
# return True
# else:
# return False
#x= input("Enter First Number")
#print(abc(x))

# write a program of nested statement.


def abc(name):
if(name[0]==("A")):
return True
else:
if (name[0]==("B")):
return True
else:
return False
x= input("Enter First Number")
print(abc(x))
Or Operator in Python
Rules or Or operator-
True or false
-True
False or false
-false
False or True
-True
def abc(abc):
Return (name[0]==(“A”) or (name[0]==(“B”) ))
print(“Enter name” + abc)
Nested if-else
Def greater (a,b):
if(a>):
return a
Else:
return b
Def greatest(a,b,c):
Return greater(greater(a,b),c)

Def greatest (a,b,c):


Return max(a,b,c) # max is built in operator.
while- loop in python
def numbers(a):
i=1
while(i<=a):
print(i)
i=i+1
a=int(input("enter number"))
print(numbers(a))
Factorial of x
def factorial(x):
result=1
while(x>=1):
result=result * x
x=x-1
return result
x=int(input("enter number to calculate factorial"))
y=(factorial (x))
print(("factorial of") + str(x) + ("is") + str(y))
Factorial of AP
def ap(a,d,l):
while (a<=1):
print (a)
a= a + d

a=int(input("Enter 1st Term of an AP:"))


d=int(input("Enter difference b/w two terms of an AP:"))
n=int(input("enter no of terms of an AP"))
l= a + n*d - d
print (ap(a,d,l))
Infinite loop, break,continue
statement
def print_numbers(x):
i=1
while True:
if(i>x):
break
print (i)
i=i+1
print(print_numbers(50))

You might also like