0% found this document useful (0 votes)
23 views4 pages

Module 1 & 2

The document discusses key concepts in programming languages including: 1. An instruction list contains all known commands and is abbreviated as IL. High-level programming languages use symbols and words that are readable to humans to express complex commands to computers. 2. A program's source code is translated into machine code by a compiler or interpreted line-by-line by an interpreter so it can be executed. 3. Variables in Python must follow naming conventions and can store different data types like integers, floating-point numbers, and strings which can be type casted. 4. Python offers shortcut operators and functions like input() to make programming more efficient. Comments are used to explain code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Module 1 & 2

The document discusses key concepts in programming languages including: 1. An instruction list contains all known commands and is abbreviated as IL. High-level programming languages use symbols and words that are readable to humans to express complex commands to computers. 2. A program's source code is translated into machine code by a compiler or interpreted line-by-line by an interpreter so it can be executed. 3. Variables in Python must follow naming conventions and can store different data types like integers, floating-point numbers, and strings which can be type casted. 4. Python offers shortcut operators and functions like input() to make programming more efficient. Comments are used to explain code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

module 1 & 2

A complete set of known commands is called an instruction list, sometimes


abbreviated to IL.
New words are created every day and old words disappear. These languages are called
natural languages.
AN ALPHABET
-builds words
A LEXIS
-(aka a dictionary) a set of words the language offers its users
A SYNTAX
-a set of rules (formal or informal, written or felt intuitively) used to determine
if a certain string of words forms a valid sentence
SEMANTICS
-a set of rules determining if a certain phrase makes sense
High-level Programming Languages
-use symbols, words and conventions readable to humans. These languages enable
humans to express commands to computers that are much more complex than those
offered by ILs.
-A program written in a high-level programming language is called a source code (in
contrast to the machine code executed by computers). Similarly, the file containing
the source code is called the source file.
COMPILATION- the source program is translated once (however, this act must be
repeated each time you modify the source code) by getting a file (e.g., an .exe
file if the code is intended to be run under MS Windows) containing the machine
code; now you can distribute the file worldwide; the program that performs this
translation is called a compiler or translator
INTERPRETATION- you (or any user of the code) can translate the source program each
time it has to be run; the program performing this kind of transformation is called
an interpreter, as it interprets the code every time it is intended to be executed;
it also means that you cannot just distribute the source code as-is, because the
end-user also needs the interpreter to execute it.
FUNCTION CAME FROM
-They may come from Python itself
-They may come from one or more of Python's add-ons named modules;
-You can write them yourself, placing as many functions as you want and need inside
your program to make it simpler, clearer and more elegant.

integers, that is, those which are devoid of the fractional part; WHOLE NUMBERS

floating-point numbers (or simply floats), that contain (or are able to contain)
the fractional part. BASTA ETO UNG MAY DECIMAL

4.0 = .4 = 4.

3 x 10 8 = 3E8

print(0.0000000000000000000001)

Output = 1e-22

Integers: Octal and Hexadecimal Numbers


There are two additional conventions in Python that are unknown to the world of
mathematics. The first allows us to use numbers in an octal representation.

If an integer number is preceded by an 0O or 0o prefix (zero-o), it will be treated


as an octal value. This means that the number must contain digits taken from the
[0..7] range only.
0o123 is an octal number with a (decimal) value equal to 83.

The second convention allows us to use hexadecimal numbers. Such numbers should be
preceded by the prefix 0x or 0X (zero-x).

0x123 is a hexadecimal number with a (decimal) value equal to 291

Operators and Their Bindings


The binding of the operator determines the order of computations performed by some
operators with equal priority, put side by side in one expression.
rint(9 % 6 % 2)

There are two possible ways of evaluating this expression:

from left to right: first 9 % 6 gives 3, and then 3 % 2 gives 1;

from right to left: first 6 % 2 gives 0, and then 9 % 0 causes a fatal error.

*Left-Sided Binding
Operators and Their Bindings: Exponentiation
print(2 ** 2 ** 3)

The two possible results are:

2 ** 2 → 4; 4 ** 3 → 64

2 ** 3 → 8; 2 ** 8 → 256 *Right Sided Binding

PHYTON VARIABLE
a name;
a value (the content of the container)
If you want to give a name to a variable, you must follow some strict rules:

the name of the variable must be composed of upper-case or lower-case letters,


digits, and the character _ (underscore)

the name of the variable must begin with a letter;

the underscore character is a letter;

upper- and lower-case letters are treated as different (a little differently than
in the real world - Alice and ALICE are the same first names, but in Python they
are two different variable names, and consequently, two different variables);

the name of the variable must not be any of Python's reserved words (the keywords -
we'll explain more about this soon).

Shortcut Operators
For example, if we need to calculate a series of successive values of powers of 2,
we may use a piece like this:
x = x * 2
You may use an expression like this if you can't fall asleep and you're trying to
deal with it using some good, old-fashioned methods:
sheep = sheep + 1
Python offers you a shortened way of writing operations like these, which can be
coded as follows:
x *= 2
sheep += 1

Leaving comments in code: why, how, and when


A remark inserted into the program, which is omitted at runtime, is called a
comment.
# This program evaluates the hypotenuse c.

# a and b are the lengths of the legs.

a = 3.0

b = 4.0

c = (a ** 2 + b ** 2) ** 0.5 # We use ** instead of square root.

print("c =", c)

The input( ) function


The input() function is able to read data entered by the user and to return the
same data to the running program.
print("Tell me anything...")

anything = input()

print("Hmm...", anything, "... Really?")


Note:

• The program prompts the user to input some data from the console (most likely
using a keyboard, although it is also possible to input data using voice or image);

• the input() function is invoked without arguments the function will switch the
console to input mode; you'll see a blinking cursor, and you'll be able to input
some keystrokes, finishing off by hitting the Enter key; all the inputted data will
be sent to your program through the function's result;

• You need to assign the result to a variable; this is crucial - missing out this
step will cause the entered data to be lost;

The result of the input( ) function


The result of the input() function is a string.
anything = input("Enter a number: ")
something = anything ** 2.0
print(anything, "to the power of 2 is", something)
Python should have given you the following output:

Traceback (most recent call last):


File ".main.py", line 4, in <module>
something = anything ** 2.0
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'

Type CastingPython offers two simple functions to specify a type of data - here
they are: int() and float().
Their names are self-commenting:

the int() function takes one argument (e.g., a string: int(string)) and tries to
convert it into an integer; if it fails, the whole program will fail too (there is
a workaround for this situation, but we'll show you this a little later);

the float() function takes one argument (e.g., a string: float(string))and tries to
convert it into a float (the rest is the same).

anything = float(input("Enter a number: "))

something = anything ** 2.0

print(anything, "to the power of 2 is", something)


console:
Enter a number: 54
54.0 to the power of 2 is 2916.0

You might also like