COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
FURTHER PYTHON | USEFUL PYTHON METHODS
Picture
WHAT ARE PYTHON MODULES
Modules are pieces of code that form small complete programs that you can use within your own code. You can use the module in a similar way that you use your own functions, you simple 'call' the module name along with any expected parameters. This volume of modules available is one of the aspects that makes Python such a great programming language.
WHAT ARE FRAMEWORKS
In computer science, a framework is a pre-written code structure that provides a common set of functionalities, libraries, and tools that developers can use as a starting point to build applications. The purpose of a framework is to provide a standardized and reusable approach to solving common problems and tasks, which helps to simplify and speed up the development process. Frameworks typically include a set of APIs, abstract classes, and design patterns that help to enforce a certain structure and methodology for building applications. Some popular examples of frameworks in web development include Ruby on Rails, Angular, and React.
SECTION 1 | TIME
The following code shows you how to get and store the current time. Then print the time in hours and minutes.
import datetime 
    
# Get the current time and store as a date/time format in the variable current_time
current_time = datetime.datetime.now() 
    
# Printing value the current hours and minutes
print ("The current time is " + str(current_time.hour) + ":" + str(current_time.minute))
Please feel free to copy and paste the code from above. 
SECTION 2 | ENVIRONMENT VARIABLES
Environment variables allow you to create variables that can be used in your code but not seen. This is a method that you can use for sensitive information such as your password. It works by saving your password locally on your computer in a environment variable then in your code telling Python to read the content of the variable when needed, thus Python would never display the sensitive information saved in the environment variable.
NOTE: Creating environment variables is different on Mac, Windows and Linux machine so you will need to follow the correct instructions or watch the relevant video below.
LINUX AND MAC USERS
1: Open Terminal
2: Go to the home directory by typing: cd (press enter)
3: Open the profile file to edit by typing: nano .bash_profile
Note:
This command may be different depending on the OS version. Some alternatives:
4: Add the variables containing the sensitive data at the top of the document, you need to give permission to export the variables by typing the word export in from of the variable, for example; export myPassword="password123"
Note:
Do not put any spaces between the variable name and equal sign
5: You can now save and exit the bash file by pressing control X
6: The environment variable is now ready to be used in your Python file. you might need to restart your Python program.

TO ACCESS THE VARIABLE FROM PYTHON
1: Import Os
2: Call the os.environ method and the use the dictionary GET method to get the content on the environment variable.
import os

password = os.environ.get("myPassword")
print (password)
WINDOWS USERS
Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.