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

pythonCSAS Tex

This document is a beginner's guide to Python programming, covering fundamental concepts such as variables, data types, user input, conditional statements, lists, loops, and functions. It includes examples and a practice exercise for a guessing game, along with suggestions for further learning. The guide emphasizes Python's simplicity and versatility in various applications like web development and data analysis.

Uploaded by

senbeth11
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 views12 pages

pythonCSAS Tex

This document is a beginner's guide to Python programming, covering fundamental concepts such as variables, data types, user input, conditional statements, lists, loops, and functions. It includes examples and a practice exercise for a guessing game, along with suggestions for further learning. The guide emphasizes Python's simplicity and versatility in various applications like web development and data analysis.

Uploaded by

senbeth11
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

Python for Beginners

A Step-by-Step Introduction

Your Name

Your School

August 29, 2025

Your Name (Your School) Python for Beginners August 29, 2025 1 / 12
What is Python?
A high-level programming language
Created by Guido van Rossum in 1991
Known for its simple, readable syntax
Used for:
Web development
Data analysis
Artificial Intelligence
Scientific computing
Automation

[Link]

Your Name (Your School) Python for Beginners August 29, 2025 2 / 12
Your First Python Program

The classic first program


1 print ( " Hello , World ! " )

print() is a function that outputs text


Text inside quotes is called a ”string”
Python executes code line by line

Your Name (Your School) Python for Beginners August 29, 2025 3 / 12
Variables and Data Types

1 # String
2 name = " Alice "
3 # Integer
Variables store information
4 age = 25 No need to declare type
5 # Float
Basic data types:
6 height = 1.75
7 # Boolean Strings (text)
8 is_student = True Integers (whole numbers)
9 Floats (decimal numbers)
0 print ( name ) Booleans (True/False)
1 print ( age )

Your Name (Your School) Python for Beginners August 29, 2025 4 / 12
Getting User Input

1 name = input ( " What is your name ? " )


2 print ( " Hello , " + name + " ! " )
3
4 age = input ( " How old are you ? " )
5 # Convert string to integer
6 age = int ( age )
7 next_year = age + 1
8 print ( " Next year you will be " , next_year )

input() gets text from the user


Returns a string, even if numbers are entered
Use int() or float() to convert to numbers

Your Name (Your School) Python for Beginners August 29, 2025 5 / 12
Conditional Statements (if/elif/else)

1 age = int ( input ( " How old are you ? " ) )


2
3 if age >= 18:
4 print ( " You are an adult " )
5 elif age >= 13:
6 print ( " You are a teenager " )
7 else :
8 print ( " You are a child " )
9
10 print ( " This runs no matter what " )

if checks a condition
elif checks another condition if the first was false
else runs if all conditions are false
Colon (:) and indentation are crucial!

Your Name (Your School) Python for Beginners August 29, 2025 6 / 12
Lists
1 # Create a list
2 fruits = [ " apple " , " banana " , " cherry " ]
3 numbers = [1 , 2 , 3 , 4 , 5]
4
5 # Access elements ( starts at 0)
6 print ( fruits [0]) # apple
7 print ( fruits [1]) # banana
8
9 # Add to list
10 fruits . append ( " orange " )
11
12 # Loop through list
13 for fruit in fruits :
14 print ( " I like " , fruit )

Lists store multiple items in order


Items can be accessed by their index (position)
Index starts at 0, not 1!
Your Name (Your School) Python for Beginners August 29, 2025 7 / 12
Loops

1 # For loop
2 for i in range (5) :
for loops: repeat a set number of times
3 print ( i ) while loops: repeat while condition is
4
true
5 # While loop
6 count = 0 range(n) generates numbers from 0 to
7 while count < 5: n-1
8 print ( count )
9 count += 1 # Same as count =
break exits a loop early
count + 1 continue skips to next iteration

Your Name (Your School) Python for Beginners August 29, 2025 8 / 12
Functions
1 # Define a function
2 def greet ( name ) :
3 print ( " Hello , " + name + " ! " )
4
5 # Call the function
6 greet ( " Alice " )
7 greet ( " Bob " )
8
9 # Function with return value
10 def square ( number ) :
11 return number * number
12
13 result = square (5)
14 print ( result ) # 25

Functions are reusable code blocks


Defined with def keyword
Can accept parameters (inputs)
Can return values (outputs)
Your Name (Your School) Python for Beginners August 29, 2025 9 / 12
Python Keywords Cheat Sheet
except - Handle exceptions
finally - Always execute code
for - For loop
from - Import specific parts
False, True - Boolean values global - Global variable
and, or, not - Logical operators if - If condition
as - Alias for imports import - Import modules
assert - Debugging tool in - Check if value exists
break - Exit loop is - Identity comparison
class - Define a class lambda - Anonymous function
continue - Skip to next iteration None - Null value
def - Define function nonlocal - Non-local variable
del - Delete object pass - Do nothing
Your Name (Your School) Python for Beginners August 29, 2025 10 / 12
Practice Exercise: Guessing Game
1 import random
2
3 # Generate random number between 1 -10
4 secret_number = random . randint (1 , 10)
5 guess = 0
6 attempts = 0
7
8 print ( " I ’m thinking of a number between 1 and 10. " )
9
10 while guess != secret_number :
11 guess = int ( input ( " Take a guess : " ) )
12 attempts += 1
13
14 if guess < secret_number :
15 print ( " Too low ! " )
16 elif guess > secret_number :
17 print ( " Too high ! " )
18 else :
19 print ( f " Congratulations ! You guessed it in { attempts } tries ! " )
Your Name (Your School) Python for Beginners August 29, 2025 11 / 12
Next Steps in Your Python Journey

Practice with small projects


Learn about dictionaries and tuples
Explore file handling (reading/writing files)
Understand error handling (try/except)
Discover Python modules (like math, datetime)
Try simple game development with Pygame
Explore web development with Flask or Django
Learn about data analysis with Pandas

Keep coding and have fun!

Your Name (Your School) Python for Beginners August 29, 2025 12 / 12

You might also like