Beruflich Dokumente
Kultur Dokumente
//hello Mom
---------------------------------------------
2)# type('a')
---------------------------------------------
3)def testMethod():
x = 10;
if (x < 10):
print("x is less then 10")
else:
print("x is greater than 10")
method = testMethod()
//x is greater than 10
def testMethod2():
x = 8;
if (x < 10):
print("x is less then 10")
else:
print("x is greater than 10")
method = testMethod2()
//x is less then 10
---------------------------------------------
4)how to clear the screen
import os
clear = lambda: os.system('cls')
clear
---------------------------------------------
5)Comments
Single line #
Multiline
'''
def testMethod2():
x = 8;
if (x < 10):
print("x is less then 10")
else:
print("x is greater than 10")
method = testMethod2()
'''
---------------------------------------------
6)print(20.8+3)
#23.8
---------------------------------------------
7)if else
is_Greeting = 'Hi'
if is_Greeting == 'Hi' or is_Greeting == 'Hello':
print("Hi,How are you")
else :
print("Sorry")
//Hi,How are you
is_Greeting = 'Oops'
if is_Greeting == 'Hi' or is_Greeting == 'Hello':
print("Hi,How are you")
else :
print("Sorry")
//Sorry
if condition_statement:
....code
else
....code
---------------------------------------------
8)Break and Continue
while True:
print("Infinite")
//Prints Infinite infinite times
with break
while True:
print("Infinite")
break
//Infinite
continue statement
for i in range(1,11):
if i == 5:
continue
print(i)
//1 (No 5 in output)
2
3
4
6
7
8
9
10
---------------------------------------------
9)Functions
Without Parameters
def firstFunction():#Defination of function
print("This is our first function")
firstFunction() #Calling of function
With Return
def functionReturnsValue():
return "I am a result!!"
return_value = functionReturnsValue()
print(functionReturnsValue())//I am a result!!
print(return_value)//I am a result!!
MultipleReturn
def MultipleReturn():
return"this is a result,",2
print(MultipleReturn())
//('this is a result,', 2)
---------------------------------------------
10)Exception Handling
#Exception Handling- Error that occurs during runtime which is not defined
print(5/0)
#ZeroDivisionError: division by zero Exception
file = open("file", r)
---------------------------------------------
11)Data input
age = input("Please enter an age: ")
print(age)
print(type(age))
//Please enter an age: 36
//36
//<class 'str'>
we have another input method namely as below
data = raw_input("Enter a number: ")
---------------------------------------------
12)File Management
#file = open(file, mode, buffering, encoding, errors, newline, closefd, opener)
file = open("D:\\pythonFile.txt","r")
print(file.read())
file.close()
//Hello!!!!
//this is the first file.
//3rd line