Simple Python Data
Values and Data Types
value - is one of the fundamental things — like a word or a number — that a program manipulates. We often refer to these values as objects and we will use the words value and object interchangeably.
objects are classified into different classes or data types:
integers (4,5,6,7, natural numbers)
floats (4.3, floating-point numbers)
strings (text, any text, even number in text) - single or double quote => absolutely no diff in Python
Type conversion functions
int - converting to integer, either float, or string that contains number, always cuts down
float - converts to float, int or string, containing number. Adding .0
to into and conversing str as needed
str - turns integers and floats into strings
Variables
Variables are names that point to particular pieces of data. In Python, we create a variable like this:
Not all values will need quote marks around them, but the format is always the same. The variable name on the left-hand side of the single equals sign and the value is on the right. This is known as: assignment statement.
Assignment statements create new variables and also give them values to refer to.
assignment token
=
should not be confused with equality
state snapshot
This kind of figure, known as a reference diagram, is often called a state snapshot because it shows what state each of the variables is in at a particular instant in time.
Variable names and keywords
can be arbitrarily long
contain both letters and digits
HAS TO BEGIN with a letter or _underscore
legal to use uppercase letters, by convention we don’t
it is common to split words with an underscore
just_like_that
no $peci@l characters
no keywords - keywords are Python syntax words. Sometimes modules have them too:
Statements and Expressions
A statement is an instruction that the Python interpreter can execute.
assignment statement
while
statementsfor
statementsif
statementsimport
statements(There are other kinds too!)
An expression is a combination of:
values
variables
operators
and calls to functions
Expressions need to be evaluated. If you ask Python to print
an expression, the interpreter evaluates the expression and displays the result.
Evaluation of an expression produces a value. A value all by itself is a simple expression, and so is a variable. Evaluating a variable gives the value that the variable refers to.
Statements, such as the assignment statement, do not return a value.
evaluation of an expression produces a value
UNDERPANTS - statement just states something and Python is like "whatever, fine, i keep it". Expression is a statement + function to do sth with all that crap. So it grabs the statement and says, "oh, this is what you want me to do with it, OK, whatever"
Operators and Operands
operand operator operand
3 + 4
List of arithmetic operators:
+
addition-
subtraction*
multiplication/
division - always results in a float10 / 5 => 2.0
%
modulus**
exponent//
floor division, integer division
Order of operations
Parentheses
()
Exponentiation
**
Multiplication/Division
*
/
//
%
Addition/Subtraction
+
-
Relational
==
!=
<=
>=
>
<
Logical
not
and
or
!!! Operators with the same precedence are evaluated from left-to-right (except exponent)
Input
It is very important to note that the
input
function returns a string value.
Reassignment
In some programming languages, a different symbol is used for assignment, such as
<-
or:=
. The intent is that this will help to avoid confusion. Python chose to use the tokens=
for assignment, and==
for equality. This is a popular choice also found in languages like C, C++, Java, and C#.
Updating Variables
increment - adding 1
decrement - subtracting 1
bumping a variable i.e. incrementing by 1
Explicitly removing a variable
Python will garbage collect unused variables but it can be explicitly removed with the del
keyword:
Last updated
Was this helpful?