Variables, expressions, and statements in Python

Installing Python

To start programming with Python, you must be sure that python is well installed in your operating system. To install python on Windows; go to: https://www.python.org/ and download the latest version of python, the current version of python is 3.10.1. After your downloading finish, install python. For those who are using Linux or MacOS they probably have already python. Then open your Terminal or Command Prompt and type: python or python3


If you have the same thing as shown in the picture above, this means python is well installed in your computer's OS.

Reserved words in Python

          Unlike human languages, the Python vocabulary is actually pretty small. We call this “vocabulary” the “reserved words”. These are words that have very special meaning to Python. When Python sees these words in a Python program, they have one and only one meaning to Python. Later as you write programs you will make up your own words that have meaning to you called variables. You will have great latitude in choosing your names for your variables, but you cannot use any of Python’s reserved words as a name for a variable.
When we train a dog, we use special words like “sit”, “stay”, and “fetch”. When you talk to a dog and don’t use any of the reserved words, they just look at you with a quizzical look on their face until you say a reserved word. For example, if you say, “I wish more people would walk to improve their overall health”, what most dogs likely hear is, “blah blah blah
walk blah blah blah blah.” That is because “walk” is a reserved word in dog language. Many might suggest that the language between humans and cats has no reserved words1.
The reserved words in the language where humans talk to Python include the
following:

and, if, else, del, assert, with, from, None, True, False, as, elif, global, nonlocal, try, except, not, while, break, import, is, class, def, for, continue, pass, lambda, await, raise, yield, async, finaly, in, or.


                               print(
'Hello world!')

That is it, and unlike a dog, Python is already completely trained. When you say “try”, Python will try every time you say it without fail. We will learn these reserved words and how they are used in good time, but for now we will focus on the Python equivalent of “speak” (in human-to-dog language). The nice thing about telling Python to speak is that we can even tell it what to say by giving it a message in quotes:

And we have even written our first syntactically correct Python sentence. Our
sentence starts with the function
print followed by a string of text of our choosing
enclosed in single quotes. The strings in the print statements are enclosed in quotes.
Single quotes and double quotes do the same thing; most people use single quotes
except in cases like this where a single quote (which is also an apostrophe) appears
in the string

Variables

One of the most powerful features of a programming language is the ability to
manipulate
variables. A variable is a name that refers to a value.
An
assignment statement creates new variables and gives them values:
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.1415926535897931
This example makes three assignments. The first assigns a string to a new variable
named
message; the second assigns the integer 17 to n; the third assigns the
(approximate) value of
π to pi.
To display the value of a variable, you can use a print statement:
>>> print(n)
17
>>> print(pi)
3.141592653589793
The type of a variable is the type of the value it refers to.
>>> type(message)
<class 'str'>
>>>
type(n)
<class 'int'>
>>>
type(pi)
<class 'float'>

Operators and operands
Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands.
The operators
+, -, *, /, and ** perform addition, subtraction, multiplication,
division, and exponentiation, as in the following examples:
20+32
hour-1
hour*60+minute
minute
/60
5
**2
(5+9)*(15-7)
There has been a change in the division operator between Python 2.x and Python
3.x. In Python 3.x, the result of this division is a floating point result:
>>> minute = 59
>>> minute/60
0.9833333333333333

Statements

A statement is a unit of code that the Python interpreter can execute. We have
seen two kinds of statements: print being an expression statement and assignment.
When you type a statement in interactive mode, the interpreter executes it and
displays the result, if there is one.

A script usually contains a sequence of statements. If there is more than one
statement, the results appear one at a time as the statements execute.
For example, the script
print(1)
x
= 2
print(x)
produces the output
1 2

Expressions

An expression is a combination of values, variables, and operators. A value all by
itself is considered an expression, and so is a variable, so the following are all legal
expressions (assuming that the variable
x has been assigned a value):
17
x x
+ 17
If you type an expression in interactive mode, the interpreter evaluates it and
displays the result:
>>> 1 + 1
2
But in a script, an expression all by itself doesn’t do anything! This is a common
source of confusion for beginners.


Comments

Post a Comment

Popular posts from this blog

HISTOIRE DE MINEMBWE ET SES AUTOCHTONES

Extracting Dates From Medical Data