Creating Python Files and Variables
Python Files
Python files (or modules) are a way to organize and reuse code. They are simply text
files with a .py extension, and can be run from the command line or imported into other
Python programs.
Creating a Python File
To create a Python file, you'll need to:
1. Open a text editor (such as Sublime Text, Atom, or Notepad)
2. Write your Python code
3. Save the file with a .py extension
For example, you might create a file called hello.py with the following code:
print("Hello, world!")
To run the file, open a command prompt or terminal window, navigate to the directory
where the file is saved, and enter the name of the file (including the .py extension).
Importing a Python File
To import a Python file into another program, you'll need to use the import statement,
followed by the name of the file (without the .py extension). For example:
import hello
This will import the hello file, and you can access its functions and variables using the
dot notation.
Variables
Variables are used to store data in a Python program. You can think of them as labels
or names for values.
Creating a Variable
To create a variable, you'll need to use an assignment statement, such as:
name = "Alice"
Python Files
Python files (or modules) are a way to organize and reuse code. They are simply text
files with a .py extension, and can be run from the command line or imported into other
Python programs.
Creating a Python File
To create a Python file, you'll need to:
1. Open a text editor (such as Sublime Text, Atom, or Notepad)
2. Write your Python code
3. Save the file with a .py extension
For example, you might create a file called hello.py with the following code:
print("Hello, world!")
To run the file, open a command prompt or terminal window, navigate to the directory
where the file is saved, and enter the name of the file (including the .py extension).
Importing a Python File
To import a Python file into another program, you'll need to use the import statement,
followed by the name of the file (without the .py extension). For example:
import hello
This will import the hello file, and you can access its functions and variables using the
dot notation.
Variables
Variables are used to store data in a Python program. You can think of them as labels
or names for values.
Creating a Variable
To create a variable, you'll need to use an assignment statement, such as:
name = "Alice"