Welcome to the second session of our Python course. In the previous session, we learned about the history and the inventor of the Python language. Today, we will learn more about some basic concepts in Python, such as constants, variables, expressions and statements. We will also see how to write and run code in an IDE (Integrated Development Environment).
Constants are values that do not change in a program. For example, the number 3.14 is a constant that represents the value of pi. In Python, constants are usually written in uppercase letters, such as PI = 3.14. However, this is not enforced by the language, and you can still change the value of a constant if you want to. It is just a good practice to use uppercase letters for constants to make your code more readable and avoid confusion.
Variables are names that refer to values that can change in a program. For example, you can create a variable called x and assign it a value of 10. Then, you can change the value of x to 20 later in the program. In Python, you do not need to declare the type of a variable before using it. You can simply use the assignment operator (=) to assign a value to a variable. For example, x = 10.
Expressions are combinations of values, variables and operators that produce a new value when evaluated. For example, x + 5 is an expression that adds the value of x and 5 and returns the result. Operators are symbols that perform certain operations on values or variables, such as + for addition, - for subtraction, * for multiplication, / for division, etc.
Statements are instructions that tell the computer what to do. For example, print(x) is a statement that prints the value of x to the screen. Statements usually end with a newline character or a semicolon (;) in Python.
To write and run code in Python, you need an IDE that
provides tools for editing, debugging and executing your code. There are many
IDEs available for Python, such as PyCharm, Visual Studio Code, Spyder, etc.
You can choose any IDE that suits your preference and needs. To run your code
in an IDE, you usually need to create a file with a .py extension and save your
code in it. Then, you can use the run or execute command in the IDE to run your
code and see the output.
Reserved Words
You cannot use reserved words as variable names / identifiers
One of the basic concepts in programming is the variable. A variable is a way of giving a name to a location in the memory where we can store some data. For example, we can create a variable called age and assign it the value 25. This means that in the memory, there is a place labeled age that holds the number 25. We can use the name age later in our program to access or modify the data stored there. We can also change the value of a variable by using a different assignment statement. For example, we can write age = 30 to update the value of age to 30. The name of the variable is chosen by the programmer, and it should be meaningful and descriptive of the data it represents.
Python Variable Name Rules
u Must start
with a letter or underscore _
u Must
consist of letters, numbers, and underscores
u Case
Sensitive
Expressions…
•
When
we string operators together - Python must know which one to do first
•
This
is called “operator precedence”
•
Which
operator “takes precedence” over the others?
•
x
= 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:
u Parentheses are always respected
u Exponentiation (raise to a power)
u Multiplication, Division, and
Remainder
u Addition and Subtraction
Left to right
•
Remember
the rules top to bottom
•
When
writing code - use parentheses
•
When
writing code - keep mathematical expressions simple enough that they are easy
to understand
•
Break
long series of mathematical operations up to make them more clear
What Does “Type” Mean?
•
In Python variables, literals, and constants have a “type”
•
Python knows the difference between an integer number
and a string
•
For example “+” means “addition” if something is a
number and “concatenate” if something is a string
|
Integer Division
Integer division produces a floating point result
Integer division produces a floating point result
Comments in Python
•
Anything after a # is ignored by Python
•
Why comment?
- Describe what is going to happen in a
sequence of code
- Document who wrote the code or other
ancillary information
- Turn off a line of code - perhaps temporarily