Posts

Showing posts from 2023

No Fluff Guide to Python - P12 - Loops

Image
 "History is about loops and continuum." while true : dispense(coldDrink_Can) Imagine you have a big box of toys: you want to go through each toy and play with it but instead of picking up each toy one by one, you want a way to play with all the toys without lifting them individually that's where loops in Python come in handy a loop is like a magical robot arm that helps you go through each toy in the box automatically it saves you time and effort. In Python: you can use a loop to repeat a set of instructions  over and over again  until a certain condition is met Example you can tell the loop to play with each toy in the box  until there are no more toys left example: toys = [ "car" , "ball" , "doll" , "blocks" ] for toy in toys: play_with(toy) The for loop goes through each toy ( toy ) in the toys list it tells our magical robot arm to play_with() each toy It will keep playing with each toy  until it has gone ...

No Fluff Guide to Python - P11 - Working with TXT Files

Image
 "a log file is a text file" To open a file in Python, you can use the built-in open() function.  open a file called "example.txt" in read mode: file = open( "example.txt" , "r" )     To read the contents of the file, you can use the read() method.   file_contents = file . read () print (file_contents) In this example, we store the contents of the file in the file_contents variable and then print it out. To write to a file, you can use the write() method. Here's an example of how you can open a file called "example.txt" in write mode and write some text to it: file = open ( "example.txt" , "w" ) file . write ( "Hello, world!" ) file . close () When working with files, it's important to understand file permissions. File permissions determine who can access the file and what actions they can perform on it. In Python, the default permissions are usually set based on the file system's default ...