Introduction To Python (Notes)
Introduction To Python (Notes)
What is Python?
Python is a multi-purpose programming language that can be used for various tasks
such as machine learning, web development, and automation.
Uses of Python
Some popular uses of Python include:
Machine Learning and AI: Python is the number one language for machine
learning and data science projects.
Web Development: Python can be used to build amazing websites using
frameworks like Django.
Automation: Python can be used to automate repetitive tasks and increase
productivity.
Installing Python
To get started with Python, you need to download and install the latest version from
the official Python website: python.org.
Page 1
Created by Turbolearn AI
print("Hello, World!")
Variables
A variable is a name given to a value. You can use variables to store data in a
computer's memory.
Declaring Variables
To declare a variable, you need to give it a name and assign it a value.
age = 20
Printing Variables
You can print the value of a variable using the print() function.
print(age)
age = 30
print(age)
Page 2
Created by Turbolearn AI
Data Types
Python has several data types, including:
String Concatenation
You can combine strings using the + operator.
name = "John"
print("Hello, " + name)
Page 3
Created by Turbolearn AI
Python has three main types of data: **numbers**, **strings**, and **boole
Python has several built-in functions for converting the types of variable
| Function | Description |
| --- | --- |
| `int()` | Converts a value to an integer |
| `float()` | Converts a value to a floating-point number |
| `bool()` | Converts a value to a boolean |
| `str()` | Converts a value to a string |
When using the `input()` function, the returned value is a string. To conv
```python
birth_year = input("Enter your birth year: ")
age = 2020 - int(birth_year)
print(age)
String Methods
In Python, a string is an object that has several methods. These methods are
functions that are specific to strings.
Page 4
Created by Turbolearn AI
"In Python, a string is an object that has several methods. These methods
are functions that are specific to strings."## String Methods
"The replace() method returns a new string with the replaced characters,
it does not modify the original string."
For example, if we try to replace 'x' with '4' in a string that does not contain 'x',
nothing will happen.
Page 5
Created by Turbolearn AI
For example, we can use the in operator to check if the word 'python' is present in a
string.
Arithmetic Operations
Arithmetic Operators
Python has the following arithmetic operators:
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division (returns a floating point number)
// Division (returns an integer)
% Modulus (returns the remainder of the division)
** Exponentiation
Page 6
Created by Turbolearn AI
Operator Description
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
//= Integer division assignment
%= Modulus assignment
**= Exponentiation assignment
For example, we can use the += operator to increment the value of a variable.
x = 10
x += 3
print(x) # Output: 13
Operator Precedence
Python has a set of rules that determine the order in which operators are applied.
These rules are similar to the rules of operator precedence in mathematics.
Comparison Operators
Comparison Operators
Python has the following comparison operators:
Page 7
Created by Turbolearn AI
Operator Description
For example, we can use the > operator to compare two values.
x = 3
y = 2
print(x > y) # Output: True
Logical Operators
Logical Operators
Python has the following logical operators:
Operator Description
For example, we can use the and operator to combine two boolean expressions.
price = 25
print(price > 10 and price < 30) # Output: True
If Statements
Page 8
Created by Turbolearn AI
If Statements
If statements are used to make decisions in Python programs. They consist of a
condition and a block of code that is executed if the condition is true.
For example, we can use an if statement to print a message based on the value of a
variable.
temperature = 35
if temperature > 30:
print("It's a hot day")
print("Drink plenty of water")
```## Conditional Statements
### If Statements
> "A block of code is a group of statements that are executed together."
```python
if temperature > 30:
print("It's hot!")
print("Stay cool!")
Else If Statements
An else if statement is used to check another condition if the initial condition is false.
Page 9
Created by Turbolearn AI
Else Statements
An else statement is used to specify a block of code to execute if none of the
previous conditions are true.
Input Output
Page 10
Created by Turbolearn AI
if unit.upper() == "K":
converted_weight = weight / 0.45
print("Weight in pounds is", str(converted_weight))
else:
converted_weight = weight * 0.45
print("Weight in kilograms is", str(converted_weight))
While Loops
A while loop is used to repeat a block of code multiple times.
The condition is specified after the while keyword and is followed by a colon (:).
The block of code to be executed is indented under the while statement.
The block of code is executed as long as the condition is true.
i = 1
while i <= 5:
print(i)
i += 1
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Page 11
Created by Turbolearn AI
Creating a List
To create a list, we use square brackets [] and add items inside them, separated by
commas.
We can also use negative indices to access elements from the end of the list.
names[0] = "Jon"
print(names) # Output: ["Jon", "Bob", "Marsh", "Sam", "Mary"]
Page 12
Created by Turbolearn AI
Slicing a List
We can select a range of values from a list using slicing.
List Methods
Method Description
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # Output: [1, 2, 3, 4, 5, 6]
numbers.insert(0, -1)
print(numbers) # Output: [-1, 1, 2, 3, 4, 5, 6]
numbers.remove(3)
print(numbers) # Output: [-1, 1, 2, 4, 5, 6]
numbers.clear()
print(numbers) # Output: []
print(len(numbers)) # Output: 0
Page 13
Created by Turbolearn AI
numbers = [1, 2, 3, 4, 5]
We can also use a while loop to iterate over a list, but it's less efficient.
numbers = [1, 2, 3, 4, 5]
i = 0
| Argument | Description |
| --- | --- |
| One argument | The ending value (exclusive) |
| Two arguments | The starting value and the ending value (exclusive) |
| Three arguments | The starting value, the ending value (exclusive), and
```python
numbers = range(5)
for number in numbers:
print(number)
Two Arguments
Page 14
Created by Turbolearn AI
When two arguments are passed to the range function, the first argument is
considered the starting value and the second argument is considered the ending
value (exclusive).
Three Arguments
When three arguments are passed to the range function, the third argument is used
as the step.
Tuples in Python
Tuples are a type of data structure in Python that are similar to lists, but are
immutable.
Tuples are defined using parentheses () instead of square brackets [] like lists.
Characteristics of Tuples
Immutable: Tuples cannot be changed once created.
No append, insert, or remove methods.
Only have count and index methods.
Method Description
Page 15
Created by Turbolearn AI
Example
numbers = (1, 2, 3)
print(numbers.count(3)) # Output: 1
print(numbers.index(2)) # Output: 1
```<script type="text/javascript">
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
}
};
</script>
<script type="text/javascript" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
Page 16