Вы находитесь на странице: 1из 6

INTRODUCTION IN PYTHON

1.Printing

Test1.py
print("Hello,Theodor")
print("What are you doing?")
days='Monday','Tuesday','Wednesday'
print(days)
paragraph = """This is a paragraph.It is
made up of multiple lines and sentences."""
print(paragraph)
word='word'
sentence="This is a sentence."
print(word,sentence)

Test2.py

counter=100
miles=1000.0 #a floating point
name="John" #a string
print (counter)
print (miles)
print (name)
print(name,miles)
a,b,c=1,2,"john"
print(a,b,c)"""

"""str='Hello world!'
print (str) #Prints complete string
print (str[0]) #Prints first character
print (str[2:5])
print (str[2:]) #Prints string starting from 3rd character
print (str*2) # Prints string two times
print (str + "TEST") # Prints concatenated string"""

"part two"
list=['abcd',123,1.2,'theodor',11.2]
print (list)
print(list[0])
print (list[1:3])
print (list[2:])
print (list * 2)
"part three"
tuple=('abcd',700,2.1,'john',1.2)
list =['abcd',700,2.1,'john',1.2]

"part three"
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


print (dict['one']) # Prints value for 'one' key
print (dict[2]) # Prints value for 2 key
print (tinydict) # Prints complete dictionary
print (tinydict.keys()) # Prints all the keys
print (tinydict.values()) # Prints all the values

Test3.py

"OPERATORS"
"PART 1--Comparison"
a=21
b=10
c=0
if(a==b):
print("Line 1 - a is equal to b")
else:
print("Line 1 - a is not equal to b")

if(a!=b):
print ("Line 2 - a is not equal to b")
else:
print ("Line 2 - a is equal to b")

a=5;
b=20;
if(a<=b):
print ("Line 3 - a is either less than or equal to b")
else:
print ("Line 3 - a is neither less than nor equal to b")

"part 2--Arithmetical operations"


a=21;b=10;c=0
c=a+b
print("Line 1 - Value of c is ",c)

c=a-b;print("Line 2 - Value of c is ",c)


c=a*b;print("Line 3 - Value of c is ",c)

a=2;b=3;c=a**b;print("Line 4 - Value of c is ",c)

if(b%a==1):
print(b,"is an odd number")
else:
print(b,"is an even number")
a=5;b=6;c=a**b;d=a*b
print("the value of c is",c,"and the value of d is ",d)

"part3--Decision making:If-else statements and loops"


a=100
if a:
print("1-Got a true expression value")
print (a)
else:
print("1-Got a false expression value")
print(a)
count = 0
while(count<9):
print ('The count is:',count)
count=count+1

print("Good bye!")

Test4.py
"part 1"
var=1
while (var<=3):
num=input("Enter a number :")
print("You entered:",num)
var=var+1

var=1
while(var<=3):
num=input("Enter a number:")
print("You entered:",num)
var=var+1
else:
print("The while loop is ove")

var=2
while(var<=5):
num=input("Enter a number:")
print(var,"is smaller or equal to 5")
var=var+1
else:
print(var,"is greater than 5")

"part 2"

"""print((80,100)",cmp(80,100)) #not working


"print("cmp(80,-100)",cmp(80,-100))#the same"""
import math
print("math.fabs(-45.17)",math.fabs(-45.17))
print("math.fabs(math.pi): ",math.fabs(math.pi))
print(math.pow(100,2))
print(math.ceil(2.1))
print(round(21.2222,2))
print(math.modf(2.12))# produces the fractional and the integer part of the
#number in this order

"part 3--accessing values in strings"

var1='Hello everybody!'
var2="Python programming"
print("var1[0]: ",var1[0])
print ("var2[1:5]: ",var2[1:5])

print("Hello my name is %s and your name is %s"%('Theodor','Jean'))


print("Hello your name is %s and you are %d years old"%('Rico',21))
"Strings---continuation"
"Capitalization"

str="this is a string"
print(str.capitalize())
str = "this is string example....wow!!!";
print ("str.center(40, 'a') : ", str.center(40, 'a'))
print(str.center(30,'b')) #this leaves the string unchanged because the
length
#of str is greater than 40

import time
localtime=time.localtime(time.time())
print(localtime)
#the result of display is time.struct_time(tm_year=2017, tm_mon=2, tm_mday=9,
#tm_hour=21, tm_min=54, tm_sec=59, tm_wday=3, tm_yday=40, tm_isdst=0)

localtime=time.asctime(time.localtime(time.time()))
print(localtime)

import calendar
cal=calendar.month(2008,1)
print("here is the calendar:",cal)

The result of calendar is here:


here is the calendar: January 2008
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

"Functions"
"In order to declare a function we must use the keyword def"
def printme(str):
print(str)
return;

printme(str="theodor")

def printinfo(name,age):
print("Name:",name)
print("Age:",age)
return;
printinfo(age=50,name="theodor")

def printinfo(name,age=30):
print("Name: ",name)
print("Age: ",age)
return;
printinfo(name="lucas")
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print ("Output is: ")
print (arg1)
for var in vartuple:
print (var)
return;

# Now you can call printinfo function


printinfo( 10 )
printinfo( 70, 60, 50 )

//The results will be:


10
And
70
60
50
"Anonymous functions"
sum=lambda arg1,arg2:arg1+arg2-1;
print("Value of total : ",sum(10,20)) #prints:Value of total: 29
print("Value of total 2: ",sum(20,20)) # prints:Value of total : 39

"With the return statement"


def sum(arg1,arg2):
total=arg1+arg2
print ("Inside the function: ",total)
return total;

total=sum(10,20);
print("Outside the function : ",total)

"Scope"
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print ("Inside the function local total : ", total)
return total;

# Now you can call sum function


sum( 10, 20 );
print ("Outside the function global total : ", total)

"For loops"

for letter in 'Javascript':


print('Current Letter :',letter)

fruits=['oranges','strawberry','mango']
for fruit in fruits:
print('Current fruit: ', fruit)

fruits=['banana','apple','mango']
for index in range(len(fruits)):
print ('Current fruit :',fruits[index])

#This will print Current fruit:banana,apple and mango on each line


s=0
for i in range(1,6):
s=s+i
print (s)#it prints the sum of the first 6-1 elements since range (a,b)
#is seen as the half-open interval [a,b)

"Lists"
list1=['the','most','beautiful']
print(list1)
del list1[1]
print(list1)

list2=[1,2,3,4,5,6,7];
print(list2[0]) # prints 1
print(list2[1:4]) # prints a slice of the list,namely 2 3 4 5

"OPerations with lists"


"length"
print (len([1,2,3]))
list=[1,2,3]+[2,3,4] # concatenates two lists
print(list)

list=[1,2]
print(list*2) # appears two times the list 1 2
for x in [1,2,3]:print(x)# prints on each row an element
list=list*3
print(list)# the list repeats itself 3 times
print(list[1:])# starting with the 2nd position it prints all elements

"Minimum and maximum"


l1,l2=['xyz','zara','abc'],[456,700,200]
print("Max value element : ",max(l1))
print("Min value element : ",min(l2))
#If l1 would be [12,'xyz','zara','abc'] the maximum would not be supported
#because there is no comparison relation between string and int

Вам также может понравиться