0% found this document useful (0 votes)
4 views3 pages

Intro to Python Programming

This document is an introductory guide to Python programming for beginners, covering fundamental concepts such as variables, data types, operators, user input, conditionals, loops, functions, lists, and comments. It emphasizes Python's readability and ease of use, providing examples and practice exercises to reinforce learning. The guide encourages daily practice and gradual exploration of more advanced topics after mastering the basics.

Uploaded by

denniskpe0dk0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Intro to Python Programming

This document is an introductory guide to Python programming for beginners, covering fundamental concepts such as variables, data types, operators, user input, conditionals, loops, functions, lists, and comments. It emphasizes Python's readability and ease of use, providing examples and practice exercises to reinforce learning. The guide encourages daily practice and gradual exploration of more advanced topics after mastering the basics.

Uploaded by

denniskpe0dk0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Python Programming

A short, beginner-friendly guide — no prior programming experience needed

1. What Is Programming?
A computer only does exactly what it is told, nothing more. Programming is the act of writing precise instructions
— called code — that tell a computer what to do, step by step. Python is a programming language: a set of rules
for writing those instructions in a way both humans and computers can understand. Python is popular because its
syntax reads almost like plain English, making it a great first language.

Running Python
You write Python code in a file ending with .py (e.g. [Link]), then run it with an interpreter. You can also try code
instantly online (e.g. [Link]) without installing anything.

Your First Program


print("Hello, World!")

print() is a built-in function that displays text on the screen. Text wrapped in quotes is called a string.

2. Variables and Data Types


A variable is a named container that stores a value in memory, so you can reuse or change it later. Python
decides the data type automatically based on the value you assign.
name = "Dennis" # str (text)
age = 21 # int (whole number)
gpa = 3.75 # float (decimal number)
is_student = True # bool (True or False)

print(name, age, gpa, is_student)


print(type(age)) # shows the data type

Rules of thumb: variable names cannot start with a number, cannot contain spaces, and are case-sensitive (age and
Age are different).

Practice Exercise 1

1 Create variables for your name, your course, and your year of study, then print them all in one line.
2 Create a variable called num1 and num2, then print their sum, difference, and product.

3. Operators
Operators let you perform calculations and comparisons.
# Arithmetic
print(10 + 3) # addition -> 13
print(10 - 3) # subtraction -> 7
print(10 * 3) # multiplication -> 30
print(10 / 3) # division -> 3.333...
print(10 // 3) # floor division -> 3
print(10 % 3) # remainder (modulus) -> 1
print(10 ** 2) # exponent (power) -> 100

# Comparison (returns True/False)


print(10 > 3) # True
print(10 == 3) # False (== means "equal to")

4. Getting Input from the User


The input() function pauses the program and waits for the user to type something. It always returns a string, so
numbers must be converted using int() or float().
name = input("What is your name? ")
age = int(input("How old are you? "))

print("Hello " + name + ", next year you will be " + str(age + 1))

Practice Exercise 2

1 Ask the user for two numbers and print their sum.
2 Ask the user for their name and age, then print: "[name] is [age] years old."

5. Making Decisions: if / elif / else


Conditionals let a program choose different actions depending on whether something is True or False. Indentation
(spaces at the start of a line) is how Python knows which code belongs inside a block.
score = 65

if score >= 70:


print("Grade: A")
elif score >= 60:
print("Grade: B")
else:
print("Grade: C")

6. Repeating Actions: Loops


Loops let you repeat code without rewriting it. A for loop repeats a fixed number of times (or over a collection of
items). A while loop repeats as long as a condition stays True.
# for loop
for i in range(1, 6):
print("Count:", i)

# while loop
total = 0
n = 1
while n <= 5:
total = total + n
n = n + 1
print("Sum 1 to 5 =", total)

Practice Exercise 3

1 Write a for loop that prints all even numbers from 2 to 20.
2 Write a while loop that keeps asking the user to enter a password until they type "python123".

7. Functions
A function is a reusable block of code that performs a task. You define it once with def, then call it whenever
needed. Functions can accept inputs (parameters) and send back a result (return value).
def greet(name):
return "Hello, " + name + "!"

def add(a, b):


return a + b

print(greet("Dennis"))
print(add(4, 7))

8. Lists and Strings


A list stores multiple values in one variable, in order, and can be changed after creation. A string is text, and can
be sliced or looped over like a list of characters.
fruits = ["apple", "banana", "mango"]
[Link]("orange") # add an item
print(fruits[0]) # apple (indexing starts at 0)
print(len(fruits)) # 4

for fruit in fruits:


print(fruit)

word = "python"
print([Link]()) # PYTHON
print(word[0:3]) # pyt (slicing)

Practice Exercise 4

1 Create a list of 5 of your favourite subjects and print each one using a for loop.
2 Write a function called is_even(n) that returns True if a number is even, and False otherwise.
3 Combine what you've learned: write a program that asks for 5 numbers, stores them in a list, and
prints the largest one.

9. Comments
Comments are notes for humans; Python ignores anything after a #. Use them to explain your code.
# This program calculates area of a circle
radius = 5
area = 3.14159 * radius ** 2

Next steps: Practice daily with small programs, get comfortable with errors (they are normal and part of learning), and
gradually explore dictionaries, file handling, and object-oriented programming once these basics feel natural.

You might also like