High-level Programming Language
What Is Python? Code Readability
Python Emphasizes Clean Syntax
Indentation Structure
Easy To Learn And Use
Introduction To Python Why Python? Extensive Library Support
Open Source
Vs Code
Ide
Pycharm
writing the first python program print("hello, world!")
spaces or tabs
Indentation And Its Importance other programming languages use braces ({}) to group code
python uses indentation to define blocks of code.
4 spaces or 1 tab.
if true:
conditionals
print("this is indented")
Where To Indent for i in range(3):
Python Syntax loops
print(i)
def greet(name):
print(f"hello, {name}") # indented
functions block
greet("alice")
# single line comment
comments in python
""" """ multi line comments
name = input("What is your name? ")
input()
print(f"Hello, {name}!")
Input/output
print() print("Hello, World!")
age = 25
price = 19.99
variable_name = value
name = "alice"
is_active = true
Variables And Data Types
integers
types of floats
datatypes strings
booleans
print(a + b)
print(a - b)
Arithmetic a = 10 print(a * b)
+, -, *, /, %, ** print(a / b)
Operators b=3
print(a % b)
print(a ** b)
print(x == y)
print(x != y)
Operators Comparison x=5 print(x > y)
==, !=, >, <, >=, <= print(x < y)
Operators y=8
print(x >= 5)
print(y <= 10)
a = 10 print(a > b and a < c)
Logical Operators and, or, not b=5 print(a < b or a < c)
c = 15 print(not (a > b))
If statement if
if
Conditional Statements Allow You To Execute Different Blocks Of Code elif statement
elif
Based On Certain Conditions
if
else elif
Control Flow else
age = 18
if age < 13:
print("you are a child.")
elif 13 <= age < 18:
print("you are a teenager.")
else:
print("you are an adult.")
Loops Are Used To Repeat A Block Of Code Multiple Times.
for i in range(1, 11):
Basics Of Python numbers = [1, 2, 3, 4, 5]
if i % 2 == 0:
Basics Of Python Using Machine Learning Project-1 for print(f"{i} is even.")
for num in numbers:
else:
print(f"number: {num}")
print(f"{i} is odd.")
Looping
count = 0
while count < 3:
while print(f"count: {count}")
count += 1
What Are The Looping Statement?
numbers = [1, 2, 3, 4, 5, 6]
for num in numbers:
if num == 4:
exit the loop prematurely print(f"Number {num} found! Exiting the loop.")
break
print(f"Checked number: {num}")
break and
continue
numbers = [1, 2, 3, 4, 5, 6]
for num in numbers:
skip the rest of the code if num == 4:
print(f"Number {num} found! Exiting the loop.")
continue
print(f"Checked number: {num}")
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
List Ordered, Mutable
fruits[1] = "blueberry"
print(fruits)
colors = ("red", "green", "blue")
print(colors[1])
Tuple Ordered, Immutable
for color in colors:
Data Structure Modules print(color)
numbers = {1, 2, 3, 4, 5}
Set Unordered,mutable
[Link](6)
print(numbers)
person = {"name": "Alice", "age": 25, "city": "New York"}
print(person["name"])
Dictionary Unordered,mutable
person["job"] = "Engineer"
print(person)
Functions Are Reusable Blocks Of Code That Perform A Specific Task. They Help Organize And Structure
Your Programs, Making Them More Modular And Easier To Maintain.
Use The Def Keyword.
Specify A Name For The Function.
How To Define A Function Add Parameters (Optional) Inside
Function Parentheses.
Write The Code Block That Runs When The Function Is
Called.
def greet():
print("hello! welcome to python.")
A Simple Function
greet()
Strings Are Sequences Of Characters
Strings s = "hello, world!"
print([Link]())
print(s[0:5])
print(len(s))
print([Link]())
Handle Errors Gracefully Using Try-except
Error Handling try:
result = 10 / 0
except zerodivisionerror:
print("cannot divide by zero!")
Python Allows You To Use Libraries With Import
Importing Modules import math
print([Link](16))