Running Python

George's Python
6 min readFeb 23, 2023

To run Python code, you need to have Python installed on your computer. You can follow the steps outlined in the previous lecture to download and install Python.

Photo by Alexander Redl on Unsplash

Once you have Python installed, you can run Python code in a few different ways:

  1. Using the Python Interpreter

The Python interpreter is a command-line tool that allows you to run Python code interactively. To open the Python interpreter, open a command prompt or terminal window and type python followed by the Enter key. You'll see the Python prompt (>>>) indicating that you can start entering Python code. Try entering the following code:

print("Hello, World!")

and hit Enter. You should see the text "Hello, World!" printed to the console.

2. Running Python Scripts

You can also write Python code in a file and then run the file from the command line. To do this, create a new file with a .py extension and enter your Python code in the file. For example, create a file called hello.py with the following code:

print("Hello, World!")

Save the file and then open a command prompt or terminal window. Navigate to the directory where you saved the file and type python hello.py followed by the Enter key. You should see the text "Hello, World!" printed to the console.

Basic syntax

Python code is written using a specific syntax that defines how the code should be structured. Here are some basic elements of Python syntax:

  1. Indentation — Python uses indentation to define code blocks. Code blocks are typically defined by loops, functions, and conditional statements. Indentation should be consistent throughout the code block.
  2. Comments — Comments in Python start with the # symbol and are used to explain what the code is doing. Comments are not executed by the Python interpreter.
  3. Variables — Variables are used to store values in Python. A variable is created by giving it a name and assigning it a value. For example:
x = 5

In this example, we create a variable called x and assign it the value 5.

Data types

Python supports several data types, including:

  1. Numbers — Numbers in Python can be integers, floating-point numbers, or complex numbers.
  2. Strings — Strings in Python are used to represent text data. Strings are created by enclosing text in single or double quotes. For example:
message = "Hello, World!"

In this example, we create a string variable called message and assign it the value "Hello, World!".

3. Lists — Lists in Python are used to store collections of items. Lists are created using square brackets and can contain any combination of data types. For example:

numbers = [1, 2, 3, 4, 5]

In this example, we create a list variable called numbers and assign it the values 1, 2, 3, 4, and 5.

Operators

Python supports a wide variety of operators for performing mathematical operations, comparisons, and logical operations. Here are some of the most common operators:

  1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. Here are some examples:

x = 5 + 3  # Addition
y = 7 - 2 # Subtraction
z = 4 * 2 # Multiplication
a = 10 / 2 # Division

In addition to the basic arithmetic operators, Python also provides the following operators:

  • Exponentiation: ** (e.g. 2 ** 3 returns 8)
  • Floor division: // (e.g. 7 // 2 returns 3)
  • Modulo: % (e.g. 7 % 2 returns 1)

Let’s see some examples of these operators in action:

# Exponentiation
x = 2 ** 3 # 2 raised to the power of 3
print(x) # Output: 8
# Floor division
y = 7 // 2 # Integer division
print(y) # Output: 3
# Modulo
z = 7 % 2 # Remainder of integer division
print(z) # Output: 1

2. Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (True or False). Here are some examples:

x = 5
y = 3
print(x > y) # Output: True (is x greater than y?)
print(x < y) # Output: False (is x less than y?)
print(x == y) # Output: False (are x and y equal?)
print(x != y) # Output: True (are x and y not equal?)

In addition to the basic comparison operators (>, <, ==, and !=), Python also provides the following operators:

  • Greater than or equal to: >=
  • Less than or equal to: <=

3. Logical Operators

Logical operators are used to combine Boolean expressions and return a Boolean value. Here are the three basic logical operators:

  • and: Returns True if both expressions are true
  • or: Returns True if at least one expression is true
  • not: Reverses the Boolean value of an expression

Here are some examples:

x = 5
y = 3
z = 7
print(x > y and z > y) # Output: True (is x greater than y AND z greater than y?)
print(x > y or z < y) # Output: True (is x greater than y OR z less than y?)
print(not x > y) # Output: False (is NOT x greater than y?)

4. Assignment Operators

In Python, assignment operators are used to assign values to variables. There are several different assignment operators available in Python, which can be used to perform different types of operations on variables. Let’s take a look at some of the most common assignment operators.

= (Simple Assignment)

The = operator is used to assign a value to a variable. For example:

x = 10

This assigns the value 10 to the variable x.

+= (Add and Assign)

The += operator is used to add a value to a variable and then assign the result back to the same variable. For example:

x = 10
x += 5
print(x) # Output: 15

This adds 5 to the value of x (which is 10) and assigns the result (15) back to x.

-= (Subtract and Assign)

The -= operator is used to subtract a value from a variable and then assign the result back to the same variable. For example:

x = 10
x -= 5
print(x) # Output: 5

This subtracts 5 from the value of x (which is 10) and assigns the result (5) back to x.

*= (Multiply and Assign)

The *= operator is used to multiply a variable by a value and then assign the result back to the same variable. For example:

x = 10
x *= 5
print(x) # Output: 50

This multiplies the value of x (which is 10) by 5 and assigns the result (50) back to x.

/= (Divide and Assign)

The /= operator is used to divide a variable by a value and then assign the result back to the same variable. For example:

x = 10
x /= 5
print(x) # Output: 2.0

This divides the value of x (which is 10) by 5 and assigns the result (2.0) back to x. Note that the result is a float, even though both operands were integers.

%= (Modulus and Assign)

The %= operator is used to calculate the modulus of a variable and a value, and then assign the result back to the same variable. For example:

x = 10
x %= 3
print(x) # Output: 1

This calculates the modulus of x (which is 10) and 3, which is 1, and assigns the result back to x.

//= (Floor Divide and Assign)

The //= operator is used to perform floor division of a variable by a value and then assign the result back to the same variable. For example:

x = 10
x //= 3
print(x) # Output: 3

This performs floor division of x (which is 10) by 3, which is 3, and assigns the result back to x.

**= (Exponentiate and Assign)

The **= operator is used to exponentiate a variable by a value and then assign the result back to the same variable. For example:

x = 2
x **= 3
print(x) # Output: 8

This raises the value of x (which is 2) to the power of 3, which is 8, and assigns the result back to x.

I hope that helps! Let me know if you have any questions.

for the Next step Click here

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

George's Python
George's Python

Written by George's Python

I write to help how to learn Python

No responses yet

Write a response