1.1 Background
1.2 Input/output
end=""
option removes the newline character after hello, producing Hello world!
on the same line.
print()
function outputs
"You entered:"
followed by a space character, followed by the input data, Sophia
, which is referenced by the variable name.
1.3 Variables
1.4 String basics
'7 days'
is also a string.
fred78@gmail.com
, is enclosed in matching double quotes, and is assigned to the variable email
. A string can contain characters such as "@" or "." in addition to letters and numbers.
len()
function returns the number of characters in the string. 6 characters exist in
"Hi Ali"
which are "H", "i", " ", "A", "l", and "i".
number_of_digits
variable's value is
2
, and as such, the output is
"Number 12 has 2 digits."
.
1
and
0
are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string
"10"
is produced. Note that concatenation operator does not include any default separator characters like the space character.
"A"
and
"wake"
are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string
"Awake"
is produced. Note that the concatenation operator does not include any default separator characters like the space character.
holiday
. Note that
"one-sided"
would assign the same string to holiday
.
1.5 Number basics
x = 1
is an integer, and when printing the type of an integer variable, the output is <class 'int'>
.
c = x - z = 7 - 2 = 5
.
Line 3 increments variable c
by 1, making the total equal to 6.
7/2 = 3.5
.
2 * 1.5 = 3.0
.
1 + 3
to be evaluated first.
-16
result can be confusing because, in math,
"-4 squared"
is
16
.
=
), the negative operator (-
), and the exponentiation operator (**
).
1.6 Error messages
1.7 Comments
Then again, inserting a blank line in a three-line program is generally unnecessary.