Read Files

You can use any file you want. First example is with.txt file.

#import files from dedicated path
#txt
GetFile = open(r"D:\Heart4DataScience\Files\boars.txt","r")
DataFile = GetFile.read()
print(DataFile)
GetFile.close()
#csv
GetFile = open(r"D:\Heart4DataScience\Files\boars.csv","r")

For special characters in the file, will be useful to use proper encoding method, by declaring this element at the end of ‘open’ function

GetFile = open(r"D:\Heart4DataScience\Files\boars.txt","r", encoding="utf-8")

If we don’t need to use outside memory we can just copy/iport file directly to our project:

And now we can operate on a file inside PyCharm project – read or write:

with open("boars.txt","r", encoding="utf-8") as GetFile:
    for flines in GetFile:
        print(flines, end="")
SaveFile =  open("boars2.txt","w", encoding="utf-8")
SaveFile.write("Boars in the city is undeniably a hot topic.")
SaveFile.close()
result for saving txt

Def_functions vol2

# *args
def function1(*args):
  return sum(args)
print(function1(1,4,5))

# *args + loop
def function1(*args):
  total = 0
  for i in args:
    total += i
  return total

print(function1(1,2,3))

results
#**kwargs
def function2(**names):
  print("My name is " + names["fname"])
  print("My last name is " + names["lname"])
  print("My full name: " + names["lname"], names["fname"])

function2(fname = "Agnieszka", lname = "Szczep")
#def const value then change it
def function3(music = "Rock"):
  print("I love " + music + " music")

function3()
function3("Hip-hop")
function3("Trance")
#math recursion model
def function4(k):
  if(k > 0):
    result = k + function4(k - 1)
    print(result)
  else:
    result = 0
  return result

print("Recursion Example Results")
function4(4)