Chapter1_PythonBasics
Chapter1_PythonBasics
1. Python Basics
2. Flow control
3. Functions
CHAPTER 1
Python Basics
Python Basics
>>> 5 +
File "<stdin>", line 1
5+
^
SyntaxError: invalid syntax
>>> 42 + 5 + * 2
File "<stdin>", line 1 42 + 5 + * 2
^
SyntaxError: invalid syntax
>>> 'Hello world!
SyntaxError: EOL while scanning string literal
>>> a=2
type(a)
The integer, floating-Point, and String
data types
■ Expressions are just values combined with operators, and they always
evaluate down to a single value. A data type is a category for values,
and every value belongs to exactly one data type.
String concatenation and
replication
■ The meaning of an operator may change based on the data types of the values next to it.
■ For example, + is the addition operator when it operates on two integers or floating-point
values.
■ >>>2+2
■ 4
■ However, when + is used on two string values, it joins the strings as the string concatenation
operator. Enter the fol- lowing into the interactive shell:
■ >>> 'Alice' + 'Bob’
■ >>>”Ashwini”+”D”
■ “AshwiniD”
■ 'AliceBob'
String concatenation and
replication
■ if you try to use the + operator on a string and an integer value, Python
will not know how to handle this, and it will display an error message.
■ >>> 'Alice’ + 42
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module> 'Alice' + 42
TypeError: Can't convert 'int' object to str implicitly
The error message Can't convert 'int' object to str implicitly means that
Python thought you were trying to concatenate an integer to the string
'Alice'. Your code will have to explicitly convert the integer to a string,
because Python cannot do this automatically.
String concatenation and
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.
■ 3*3
■ >>> 'Alice’ * 5
■ 'AliceAliceAliceAliceAlice’
■ The expression evaluates down to a single string value that repeats the
original a number of times equal to the integer value.
■ String replication is a useful trick, but it’s not used as often as string
concatenation.
String concatenation and
replication
■ The * operator can be used with only two numeric values (for
multiplication) or one string value and one integer value (for string
replication). Otherwise, Python will just display an error message.
■ >>> 'Alice' * 'Bob'
Traceback (most recent call last):
■ File "<pyshell#32>", line 1, in <module> 'Alice' * 'Bob' TypeError:
can't multiply sequence by non-int of type 'str' >>> 'Alice' * 5.0
Traceback (most recent call last):
■ File "<pyshell#33>", line 1, in <module> 'Alice' * 5.0 TypeError: can't
multiply sequence by non-int of type 'float'
Storing Values variable in Variables
>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
>>> spam = spam + 2
>>> spam
42
Assignment
Statements with
string
■ You can close the file editor by clicking the X at the top of the window.
To reload a saved program, select File->Open from the menu. Do
that now, and in the window that appears, choose hello.py, and click
the Open button.
■ You can close the file editor by clicking the X at the top of the window.
To reload a saved program, select File4Open from the menu. Do that
now, and in the window that appears, choose hello.py, and click the
Open button. Your previously saved hello.py program should open in
the file editor window.
■ Your previously saved hello.py program should open in the file editor
window.
Dissecting your Program
■ Comments
# This program says hello and asks for my name.
The print() Function
print('Hello world!')
print('What is your name?') # ask for their name
■ The input() Function
■ The input() function waits for the user to type some text on the keyboard
and press enter.
■ myName = input()
Dissecting your Program