Module 1.1
Module 1.1
Module 1.1
Python Basics: Entering Expressions into the Interactive Shell, The Integer, Floating-Point, and
String Data Types, String Concatenation and Replication, Storing Values in Variables, Your
First Program, Dissecting Your Program
The integer (or int) data type indicates values that are whole numbers.
Numbers with a decimal point, such as 3.14, are called floating-point numbers (or
floats).
Always surround your string in single quote (') characters (as in 'Hello') so Python knows
where the string begins and ends. You can even have a string with no characters in it, '',
called a blank string.
However, when + is used on two string values, it joins the strings as the string
concatenation operator.
Eg:
Replication
The * operator is used for multiplication when it operates on two integer or floating-point
values. But when the * operator is used on one string value and one integer value, it
becomes the string replication operator.
Eg:
The expression evaluates down to a single string value that repeats the original to a number
of times equal to the integer value.
>>> 'Alice' * 'Bob' It will return a syntax error
>>> 'Alice' * 5.0 It will return a syntax error
A variable is like a box in the computer’s memory where you can store a single value.
Assignment Statements
Variable Names
You can name a variable anything as long as it obeys the following three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.
Variable names are case-sensitive, meaning that spam, SPAM, Spam, and sPaM are four
different variables.
Output
Hello world!
What is your name?
Al
It is good to meet you, Al
The length of your name is:
2
What is your age?
4
You will be 5 in a year.
The print() function displays the string value inside the parentheses on the screen.
print('Hello world!')
print('What is your name?') # ask for their name
When Python executes this line, you say that Python is calling the print() function and the
string value is being passed to the function. A value that is passed to a function call is an
argument.
Notice that the quotes are not printed to the screen. They just mark where the string begins
and ends; they are not part of the string value.
You can also use this function to put a blank line on the screen; just call print() with nothing
in between the parentheses.
The input() function waits for the user to type some text on the keyboard and press enter.
myName = input()
This function call evaluates to a string equal to the user’s text, and the previous line of code
assigns the myName variable to this string value.
You can think of the input() function call as an expression that evaluates to whatever string
the user typed in. If the user entered 'Al', then the expression would evaluate to myName =
'Al'.
The following call to print() actually contains the expression 'It is good to meet you, ' +
myName between the parentheses.
print('It is good to meet you, ' + myName)
Remember that expressions can always evaluate to a single value. If 'Al' is the value stored
in myName on the previous line, then this expression evaluates to 'It is good to meet you,
Al'. This single string value is then passed to print(), which prints it on the screen.
You can pass the len() function a string value (or a variable containing a string), and the
function evaluates to the integer value of the number of characters in that string.
print('The length of your name is:')
print(len(myName))
Just like those examples, len(myName) evaluates to an integer. It is then passed to print() to
be displayed on the screen. Notice that print() allows you to pass it either integer values or
string values.
>>> len('hello')
5
>>> len('My very energetic monster just scarfed nachos.')
46
>>> len('')
0
Because str(29) evaluates to '29', the expression 'I am ' + str(29) + ' years old.' evaluates to 'I
am ' + '29' + ' years old.', which in turn evaluates to 'I am 29 years old.'. This is the value that
is passed to the print() function.
The str(), int(), and float() functions will evaluate to the string, integer, and floating-point
forms of the value you pass, respectively.
The str() function is handy when you have an integer or float that you want to
concatenate to a string.
The int() function is also helpful if you have a number as a string value that you want to
use in some mathematics.
For example, the input() function always returns a string, even if the user enters a
number.
o Eg:
The value stored inside spam isn’t the integer 101 but the string '101'. If you want to do
math using the value in spam, use the int() function to get the integer form of spam and then
store this as the new value in spam.
Now you should be able to treat the spam variable as an integer instead of a string.
The int() function is also useful if you need to round a floating-point number down.
The myAge variable contains the value returned from input(). Because the input() function
always returns a string (even if the user typed in a number), you can use the int(myAge)
code to return an integer value of the string in myAge. This integer value is then added to 1
in the expression int(myAge) + 1.
The result of this addition is passed to the str() function: str(int(myAge) + 1). The string
value returned is then concatenated with the strings 'You will be ' and ' in a year.' to evaluate
to one large string value. This large string is finally passed to print() to be displayed on the
screen.