Python Intro Module1, 2
Python Intro Module1, 2
Creative and Rewarding Activity: Programming allows you to build useful, elegant, and clever
programs.
Motivations: - Problem-solving for data analysis - Career opportunities (financially and personally
rewarding) - Helping others solve problems - Personal productivity in handling data
Core Components: - Central Processing Unit (CPU): Executes instructions; measured in GHz (billions
of operations per second). - Main Memory (RAM): Fast, temporary storage for active data; volatile (data
lost when power is off). - Secondary Memory: Permanent storage like hard drives or flash memory;
non-volatile. - Input/Output Devices: Keyboard, mouse, monitor, speaker, etc. - Network Connection:
Allows remote data transfer, often slower and less reliable.
Two Essential Skills: 1. Programming Language Knowledge: Vocabulary and syntax of Python. 2.
Problem-Solving Ability: Logical combination of instructions to achieve results.
Key Concepts: - Python has a small set of reserved words. - Variables are user-defined names for data. -
Programs are sequences of instructions that solve problems.
Interactive Mode:
print('Hello world!')
1
1.5 Programming Terminology
Basic Data Types: - int: Integer numbers (2, -5) - float: Decimal numbers (3.14, -2.5) - str: String of
characters ('Hello')
Variable Assignment:
Variable Naming Rules: - Must contain letters, numbers, or underscores. - Cannot start with a number.
- Case-sensitive. - Cannot use keywords. - Use meaningful names.
Arithmetic Operators:
20 + 32 # Addition
hour - 1 # Subtraction
hour * 60 # Multiplication
2
minute / 60 # Division
5 ** 2 # Exponentiation
Modulus Operator:
String Operations:
first = '100'
second = '150'
print(first + second) # '100150'
Getting Input:
Adding Comments:
Boolean Values:
3
>>> 5 == 5
True
>>> 5 == 6
False
>>> type(True)
<class 'bool'>
Simple if Statement:
if x > 0:
print('x is positive')
if x % 2 == 0:
print('x is even')
else:
print('x is odd')
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal')
Nested Conditionals:
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
4
2.3 Exception Handling
2.5 Functions
Math Functions:
import math
[Link](5)
math.log10(100)
[Link](45)
Random Numbers:
import random
[Link]()
[Link]([1, 2, 3])
5
def print_lyrics():
print("I'm a lumberjack, and I'm okay")
print("I sleep all night and I work all day")
Function Calls:
def repeat_lyrics():
print_lyrics()
print_lyrics()
repeat_lyrics()
def print_twice(bruce):
print(bruce)
print(bruce)
print_twice('Spam')
print_twice([Link])
x = [Link](radians)
golden = ([Link](5) + 1) / 2
x = addtwo(3, 5) # x becomes 8
6
2.9 Why Use Functions?
Key Concepts Covered: - Python fundamentals and syntax - Conditional logic and flow control -
Function creation and usage - Error handling techniques - Problem-solving strategies