Python Unit 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

PYTHON BASICS

• Entering Expressions into the Interactive Shell


>>> 2 + 2
4
>>> 2
>>> 3*6
18
• You can try with the remaining mathematical functions.
Math Operators Precedence
Order of Operations
Syntax Error Messages
Integer, Floating Point and String Data Types
String Concatenation
• >>> 'Alice' + 'Bob’
'AliceBob’
• >>> 'Alice' + 42
Traceback (most recent call last): File "", line 1, in 'Alice' + 42
TypeError: can only concatenate str (not "int") to str
String Replication
• >>> 'Alice' * 5
'AliceAliceAliceAliceAlice’
• >>> 'Alice' * 'Bob’
Traceback (most recent call last):
File "", line 1, in
'Alice' * 'Bob’
TypeError: can't multiply sequence by non-int of type 'str'
Storing Values in Variables
Variable Names
Sample Program
• # This program says hello and asks for your name.
• print('Hello, world!’)
• print('What is your name?') # ask for their name
• myName = input()
• print('It is good to meet you, ' + myName)
• print('The length of your name is:') print(len(myName))
• print('What is your age?') # ask for their age
• myAge = input()
• print('You will be ' + str(int(myAge) + 1) + ' in a year.')
Dissecting Your Program
• Comments
• The print() Function
• The input() Function
• Printing the User’s Name
• The len() Function
• The str(), int(), and float() Functions
Flow Chart
Flow Control
• Boolean Values
• >>> spam = True
>>> spam
True
• Comparison Operators
Mixing Boolean and Comparison Operators
Elements of Flow Control
• if Statements
• if name == 'Alice’:
• print('Hi, Alice.’)
• else Statements
• if name == 'Alice’:
• print('Hi, Alice.’)
• else:
• print('Hello, stranger.')
Elements of Flow Control
• elif Statements
• if name == 'Alice’:
• print('Hi, Alice.’)
• elif age < 12:
• print('You are not Alice, kiddo.’)

You might also like