Академический Документы
Профессиональный Документы
Культура Документы
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"
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")
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)
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"
var1='Hello everybody!'
var2="Python programming"
print("var1[0]: ",var1[0])
print ("var2[1:5]: ",var2[1:5])
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)
"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;
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;
"For loops"
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])
"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
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