Sie sind auf Seite 1von 5

1)print("hello Mom")

//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.

To read specific amount of character/bytes


file = open("D:\\pythonFile.txt","r")
print(file.read(4))
file.close()
//Hell

To know the cursor is which position in file -- tell


print(file.tell())
//4

To move the pointer to a specific place -- seek


file.seek(15) //Will move the cursor to 15th position
print(file.tell()) //Returns 15

To read a file one line a time


for line in file:
print(line)
file.close()
// Hello!!!!

//this is the first file.

//this is 2nd line

//3rd line

Getting some file info and attributes


print("File Name: "+file.name)
print("is Closed: "+str(file.closed)) //Boolean so converted to string
print("Model "+file.mode)
//File Name: D:\pythonFile.txt
//is Closed: False
//Model r
---------------------------------------------
13)File Writing
file = open("D:\\pythonFileWrite.txt","w+") //write mode either creates a new file
if file not available
file.write("Hello Python this is my first file!!!")
file.seek(0) //To replace it from 0th position
file.write("Nitish ") //Will add at 0th position'
file.seek(0) //To read the file from 0th position if not added with write call the
pointer is at end of "Nitish"
print(file.read())

//Nitish ython this is my first file!!!


---------------------------------------------
14)Dictionary
Collection of key/value pair as maps in java
my_dictionary = {'Key':'Value',('K','E','Y'):5}
my_dictionary2 = {x:x+1 for x in range(10)}
print(my_dictionary)
print(my_dictionary2)
//{'Key': 'Value', ('K', 'E', 'Y'): 5}
//{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10}
Order is not restored and is random
To retrieve a particular value using a key is as
print(my_dictionary[('K','E','Y')]) // 5

To retrieve an element which is not available in dictionary


try:
print(my_dictionary2[12])
except Exception as e:
print(e)
//prints 12

To retrieve list of keys and values


print(my_dictionary.keys()) //dict_keys(['Key', ('K', 'E', 'Y')])
print(my_dictionary.values()) //dict_values(['Value', 5])

To add an element to dictionary


my_dictionary[1]=2 //Ramdomly added to any position with key=1 and value =2
print(my_dictionary) //{'Key': 'Value', ('K', 'E', 'Y'): 5, 1: 2}

To delete an element to dictionary


del my_dictionary[1]
print(my_dictionary)
//{'Key': 'Value', ('K', 'E', 'Y'): 5}

To remove all elements of dictionary


my_dictionary.clear()
---------------------------------------------

15)Modules and packages


in modules.py in the same package
def IsOdd(n):
if n%2 == 0:
return False;
else:
return True;

Another file in the same package


main.py
import modules as mod //refers to modules.py
print(mod.IsOdd(36))

for particular fuction import


from modules import IsOdd //same as above
print(IsOdd(36))
dir() -- lists all the fuction in the specified module
import modules
print(dir(modules))
//['IsOdd', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__']

##############Packages are not clear##################

Das könnte Ihnen auch gefallen