Integer data type:
Ex: -1, 0, 1
, 99, -55641
Floating-point data type:
Ex: 1.0, -23.45, 0.0
String data type :
Ex: 'Jane', "New York"
Boolean data type :
Ex: True, False
Assign it to a variable:
p
assword = 'caput'
score = 5
temp = 96.8
game_over = True
Add comments with a hash sign :
print('Hi') # This is a comment.
# This is another comment
.
To print a string:
print('Hello, World')
print("What's cooking?")
To add a new line, use \n inside the quotes:
print('\nHello, World')
To print the contents of a variable:
password = 'caput'
print(password)
score = 5
print(score)
temp = 96.8
print(temp)
game_over = True
print(game_over)
Addition
price1 = 20
price2 = 3
total = price1 + price2
Subtraction
difference = price1 - price2
Multiplication
qty = 5
total = price1 * qty
Division
total = 200
num_players = 5
score = total / num_players
String to int:
counter = int('10')
String to float:
price = float('6.99')
int to string:
string20 = str(20)
Printing string with another type:
age = 7 # Can be an int or a float
print('My age is ' + str(age))
String input:
name = input('Enter your name: ')
Integer input.
age = int(input('Enter your age: '))
Float input:
temp = float(input("What's the temperature?"))
Remember, input() always returns a string!
Strictly greater than:
print(10 > 8) # Prints True
print(8 > 10) # Prints False
Strictly less than:
print(6.6 < 8.8) # Prints True
print(8 < 6) # Prints False
Equal:
print(3 == 3) # Prints True
print(4.0 == 3.0) # Prints False
Not equal:
print(7 != 5) # Prints True
print(7 != 7) # Prints False
Greater than or equal:
print(9 >= 1) # Prints True
print(1 >= 9) # Prints False
Less than or equal:
print(8 <= 9) # Prints True
print(9 <= 8) # Prints False
Are they equal?
print("one" == "one") # Prints True
print("one' == 'two') # Prints False
Are they not equal?
print("one" != "one") # Prints False
print("one' != 'two') # Prints True
String concatenation :
first_name = 'Mary'
last_name = 'Jane'
print(first_name + ' ' + last_name)
String length
message = 'Hello'
print(len(message)) # Prints 5
Indexing
msg = 'Hello'
print(msg[0]) # Prints H
lower()
name = 'Mary'
print(name.lower()) # Prints mary
upper()
name = 'Mary'
print(name.upper()) # Prints MARY
isalpha()
passcode = 'abcAbdf'
print(passcode.isalpha()) # Prints True
passcode = '12343'
print(passcode.isalpha()) # Prints False
isnumeric()
score = 123
print(score.isnumeric
()) # Prints True
msg = 'hello'
print(msg.isnumeric
()) # Prints False
isalnum()
password = 'jdsds1213'
print(password.isalnum
()) # Prints True
password = 'Hello World'
print(password.isalnum
()) # Prints False
isupper()
name = 'KING'
print(name.is
upper()) # Prints True
name = 'robert'
print(name.is
upper()) # Prints False
islower()
greeting = 'hello'
print(greeting.is
lower()) # Prints True
greeting = 'NAMASTE'
print(greeting.is
lower()) # Prints False
find()
greeting = 'hello'
print(greeting.find
('h')) # Prints 0
greeting = 'hello'
print(greeting.find
('a')) # Prints -1
replace()
name = 'Jake'
print(name.replace
('a', 'o')) # Prints Joke
name = 'Jake'
print(name.replace
('b', 'o')) # Prints Jake
Simple if:
a = 1
b = 2
if a < b:
print('a is less than b')
if/else:
a = 1
b = 2
if a < b:
print('a is less than b')
else:
print('b is less than a')
Know your exit condition?
number = 5
while number >= 0:
print(number)
number = number - 1
Don’t know your exit condition?
while True:
passwd = input('Enter password:')
if passwd == 'python': print('Correct password!')
break
else:
print('Try again'
)
Syntax Error – Bad Token
Check if the quotations and parentheses match up.
Syntax Error – Bad Input
Fix any extra spaces, add any missing quotes, fix any bad indentation.
Indentation Error – Unexpected Indent
Check your indentation at the listed line number.
Name Error
Check if the program uses any variable before creating it.
Value Error
Make sure there are only numbers in the string you are trying to convert. No alphabets or symbols allowed.
Type Error
Make Python happy by converting the contents of the variable to the appropriate data type.
Division By Zero Error
Just don't divide by zero :)
Head over to Python docs