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:
Open a text editor (such as Sublime Text, Atom, or Notepad)
Write your Python code
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"
age = 25
In this example, we've created two variables: name, which stores the string
"Alice", and age, which stores the integer 25.
Types of Variables
Python has several built-in data types that you can use to store different kinds
of data:
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:
Open a text editor (such as Sublime Text, Atom, or Notepad)
Write your Python code
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"
age = 25
In this example, we've created two variables: name, which stores the string
"Alice", and age, which stores the integer 25.
Types of Variables
Python has several built-in data types that you can use to store different kinds
of data: