Python 3 Basics
#This line is commented
'''
Developer:
Date:
Contact:
'''
'''
print ("Hello Python!")
#input and print
thetext = input("Enter some text: ")
print ("This is what you entered: ")
print (thetext)
# \n within quote is for a new line
thetext = input("Enter some text\n")
print ("This is what you entered:")
print (thetext)
# another method
prompt = "Enter some text "
thetext = input(prompt)
print ("This is what you entered:")
print (thetext)
# integer, floating point number and operands
total = 0.0
number1=float(input("Enter the first number: "))
total = total + number1
number2=float(input("Enter the second number: "))
total = total + number2
average = total / 2
print ("The average is " + str(average))
print (average)
# Control statement
total = 0.0
count = 0
while count < 2:
number=float(input("Enter a number: "))
count = count + 1
total = total + number
average = total / count
print ("The average is " + str(average))
# if else statement
myNum = int(input("Enter the first number: "))
if myNum < 10:
print('entered number is less than 10.')
elif myNum > 10 and myNum < 50:
print('entered number is between 10 and 50.')
else:
print('entered number is more than 50.')
print('square of entered number is = ',str(myNum*myNum))
# type of data
total = 10
print (total)
print (type (total))
# operands on integer
print (2 + 4)
print (6 - 4)
print (6 * 3)
print (6 / 3)
print (6 % 3)
print (6 // 3) # floor division: always truncates fractional remainders
print (-5)
print (3**2) # three to the power of 2
# operands on integer
print (2.0 + 4.0)
print (6.0 - 4.0)
print (6.0 * 3)
print (6.0 / 3.0)
print (6.0 % 3.0)
print (6.0 // 3.0) # floor division: always truncates fractional remainders
print (-5.0)
print (3.0**2.0) # three to the power of 2
# mixing data types in expressions
# mixed type expressions are "converted up"
# converted up means to take the data type with the greater storage
# float has greater storage (8 bytes) than a regular int (4 bytes)
print (2 + 4.0)
print (6 - 4.0)
print (6 * 3.0)
print (6 / 3.0)
print (6 % 3.0)
print (6 // 3.0) # floor division: always truncates fractional remainders
print (-5.0)
print (3**2.0) # three to the power of 2
# these are Boolean expressions which result in a value of
# true or false
# Note that Python stores true as integer 1, and false as integer 0
# but outputs 'true' or 'false' from print statements
print (7 > 9)
print (10 < 16)
print (5 == 5)
print (6 <= 6)
print (6 >= 6)
print (10 != 10)
a = 'Hello Python. '
b = "Where are you?"
c = a + b
print (c)
# d = c + 10
# you cannot concatenate a string and an integer
# you must convert the integer to a string first:
d = c + str(100)
print (d)
# Using function without arguments
def mysum():
a = input("Enter value of A = ")
b = input("Enter value of B = ")
s = int(a) + int(b)
return s
s = mysum()
print("The sume is = ", s)
# Using function with arguments
def calc(a, b):
s = int(a) + int(b)
m = int(a) * int(b)
return s,m
a = input("Enter value of A = ")
b = input("Enter value of B = ")
s = int(a) + int(b)
s,m = calc(a, b)
print("The multiplication is = ", m)
import os
# Check current working directory.
cwd = os.getcwd()
print("Current working directory %s" % cwd)
# Now change the directory
path = cwd + "\satish"
os.chdir(path)
# Check current working directory.
newPath = os.getcwd()
print("Directory changed successfully %s" % newPath)
# Open and read file
fp = open('myFile.txt','r')
# fp = open('D:\\vsat2k\\myFile.txt','r')
string = fp.readline()
string = fp.readline()
string = fp.read(10)
string = fp.read(12)
print (string)
# Open, write and close file
fp = open('myFile.txt','w')
# fp = open('D:\\vsat2k\\myFile.txt','r')
print (fp) # prints out details about the file
fp.write("Today is Thu\n")
fp.write("I am learning Python.\n")
fp.close()
# File open, read and write
def makeCopy(oldFile, newFile):
fp1 = open(oldFile, "r")
fp2 = open(newFile, "w")
while 1:
content = fp1.read(50)
if content == "":
break
fp2.write(content)
fp1.close()
fp2.close()
return
file1 = "myFile.txt" # existing file
file2 = "copy_myFile.txt" # copy of existing file
makeCopy(file1, file2)
import os
filename = input('Enter a file name: ')
try:
f = open (filename, "r")
print ('File opened successfuly', filename )
r.read()
except:
print ('There is no file named', filename )
# creating and using a Python list
result = [0,0,0,0,0,0,0,0]
print (result)
result[0] = 17
result[1] = 20
result[5] = 50
print (result)
print (result[0])
print (result[1])
print (result[2])
print (result[3])
print (result[4])
print (result[5])
print (result[6])
print (result[7])
# Append operation
myList = []
print (myList)
myList.append(100)
print (myList[0])
myList.append("vsat2k")
print (myList)
print (myList[0])
print (myList[1])
# the following statement would generate an out-of-range error
#print (myList[2])
# accessing the last item in a list
list1 = [1,2,3,6,7,8,9,10]
print (list1)
print (list1[0])
print (list1[1])
print (list1[-1])
print (list1[-2])
# deleting items from a list
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1)
del list1[0]
del list1[-1]
print (list1)
#repeating lists
list1 = [1,2,3]
print (list1)
print (list1 * 3)
print (list1)
list1 = list1 * 2
print (list1)
# concatenating lists
list1 = [1,2,3]
print (list1)
list2 = [4,5,6]
print (list2)
list1 = list1 + list2
print (list1)
list1 = list1 + list1
print (list1)
'''
# String operations
print ("This is A")
print ("This is B's A")
# You can also print a " within a string enclosed in single quotes:
print ('One double quote ", and "within double quote" ')
# multiplying numbers and strings
print (3 * 4)
print (30 * 4)
print ("3" * 4)
print ("30" * 4)
# string concatenation
print ("vsat2k " + "is " + ("email id " * 3))
# string indexing
s1 = "Satishkumar Varma"
print (s1[0],s1[5])