Module 1
Chapter 1- Python Basics
Text book: Automate the Boring Stuff with Python By AL Sweigart
Entering Expressions into Interactive Shell
Step1: Type IDLE in the search bar and it displays the following window
Step2 :Click Open option to open IDLE shell
Expressions
Precedence of an operator is
Highest to lowest
Note: Use parentheses to
override the usual precedence
Expression Evaluation
Note: All Arithmetic operators will have Left to right
associativity if expression uses operators of equal
precedence
Note:
For Example : * Expressions are just values combined with
>>>2+4*4-1 operators and they always evaluate down to a single
17 value .
* Data type is a category for values, Every value
>>>2+4-4+1
belongs to exactly one data type
3
OR Syntax Error: incomplete input
Note:
+ is the concatenation operator if u use with strings
always Surround your string in single quotes (‘) to
know where the string begins and ends .
Blank string is represented as ‘ ‘
Note: * operator does the
String Replication
Points need to be remember w.r.t Replication operator
Variables
Variables are the names you give to computer memory locations which are used to store values in a
computer program
you can store values in variables with an assignment statement
A variable is initialized or created the first time a value is stored in it. After that you can use it in
expressions with other variables and values.
When a variable is assigned a new value the old value is forgotten this is called overwriting the variable or
updating the variable.
>>> spam = spam + eggs
Storing Values in variables
>>> spam
>>> spam = 40 42
>>> spam
>>> spam = 'Hello'
40
>>> spam
>>> eggs = 2
>>> spam + eggs 'Hello'
42
>>> spam + eggs + spam >>> spam = 'Goodbye'
82 >>> spam
'Goodbye'
Variable Names
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.
4) Names are case sensitive
Valid and invalid variable names
First Program
-- open the file editor in IDLE, Select File New File
-- Interactive shell window will always be the one
with >>> prompt but file editor does not have
Output
#program to print "hello” and accepts "name “ from the user
print('hello world!...') #prints the value to a stream or [Link] to default
print('What’s your name')
myName=input() #Read a string from standard input. the trailing newline is stripped
print('It is good to meet you, '+myName)
print('The total number of characters in your name is :'+str(len(myName)))
Print( )
#print('What is your age') # commenting out code for temporary removal
myAge=input()
print('You will be '+ str(int(myAge)+1) +' in a year')
Predict the output for the following
print(len(''))
print(len('my name is'))
print(len(‘ ‘))
Output :
0
10
1
The str(), int() and float() functions
-- Can convert a string ( str ) to an integer ( int ) and a floating point number ( float ) using int()
and float() . Use str() to convert an integer or floating point number to a string.
int(7.7)+1
8
Text and Number Equivalence
>>>42=='42'
False
>>>42==42.0
True
>>>42.0==00042.000
True
>>>'45'==45
False
Test Your Skills
1. Which of the following are operators, and which are values? 7. What should the following two expressions evaluate
'hello‘ -88.8 - / + 5 to?
>>>'spam' + 'spamspam'
>>>'spam' * 3
2. Which of the following is a variable, and which is a string?
spam 8. Why is eggs a valid variable name while 100 is
'spam‘ invalid?
3. Name three data types. 9. What three functions can be used to get the integer,
floating-point number, or string version of a value?
4. What is an expression made up of? What do all expressions do?
10. Why does this expression cause an error? How can
5. This chapter introduced assignment statements, like spam = 10.
What is the difference between an expression and a statement? you fix it?
>>>'I have eaten ' + 99 + ' berries.'
6. What does the variable bacon contain after the following code
runs?
bacon = 20
bacon + 1
Next Topic : Flow Control