What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest of
the line:
Example
print("Hello, World!") #This is a comment
A comment does not have to be text that explains the code, it can also be used
to prevent Python from executing code:
Example
#print("Hello, World!")
print("Cheers, Mate!")
Python Variables
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Example
x = 5
y = "John"
print(x)
print(y)
• Correct Example:
print('It\'s his "BIG" dog')
• The backslash (\) tells Python to treat the single quote (') as part of the text.
This ensures the program correctly handles user input and text formatting!
Notes on Data Types and Variables in Python
• Different Data Types in Python:
• Strings – Sequences of characters (e.g., "John", "Hello").
• Integers – Whole numbers (e.g., 2, 10, -5).
• Floating-point numbers (floats) – Numbers with decimal points (e.g., 2.3, 5.44).
• Python Automatically Assigns Data Types Based on Values:
• pi = 3.14 → Python assigns it as a float.
• radius = 6 → Python assigns it as an integer.
• name = 'John' → Python assigns it as a string.
Data Types and Input Prompts
• By default, input() treats all user inputs as strings.
• To specify a data type, we use typecasting (casting).
Examples of Typecasting:
• Reading an integer input:
number = int(input('Enter a number: '))
• This ensures the input is stored as an integer.
• Reading a floating-point input:
decimal = float(input('Enter a decimal number: '))
• This ensures the input is stored as a float.
Typecasting helps ensure the correct data type is used when performing calculations in Python.
Notes on Adding Numbers, Constants, and Data Types in Python
Adding Integers
• If you add two integers, the result is also an integer.
• Example program:
first_number = 5
second_number = 7
sum = first_number + second_number
print(sum) # Output: 12
• To allow user input:
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
sum = first_number + second_number
print("The sum of " + str(first_number) + " and " + str(second_number) + " is " + str(sum))
• If user inputs 5 and 7, output:
The sum of 5 and 7 is 12
Adding Floating-Point Numbers
• If you add two floating-point numbers, the result is a floating-point number.
• Example program:
first_number = float(input("Enter the first decimal number: "))
second_number = float(input("Enter the second decimal number: "))
sum = first_number + second_number
print("The sum of " + str(first_number) + " and " + str(second_number) + " is " + str(sum))
• If user inputs 3.3 and 5.4, output:
The sum of 3.3 and 5.4 is 8.7
Constants in Python
• A constant is a value that does not change throughout the program.
• Example constants:
pi = 3.14159 # Represents π (pi)
discount = 0.9 # 10% discount (100% - 10% = 90%)
vat = 1.2 # VAT of 20% (100% + 20% = 120%)
• Using constants in calculations:
original_cost = float(input("Enter original cost: "))
discounted_cost = original_cost * discount # Applying 10% discount
print("Discount price is", discounted_cost)
• If user inputs 20, output:
Discount price is 18.0
Checking in: Understanding Data Types
1. What data type is '013314567'?
• String (str), because it is enclosed in quotes.
2. What data type is 123456?
• Integer (int), because it is a whole number.
3. What data type is -3.15?
• Floating-point number (float), because it has a decimal point.
4. How do we tell Python that the data is a string?
• Enclose the value in quotes ('Hello' or "Hello").
5. How do we tell Python to expect an integer?
• Use int() when taking user input:
number = int(input("Enter an integer: "))
6. How do we tell Python to expect a real number (float)?
• Use float() when taking user input:
decimal_number = float(input("Enter a decimal number: "))