0% found this document useful (0 votes)
5 views10 pages

Python Beginner

This document provides an introduction to Python programming for beginners, covering its features, syntax, and basic concepts such as variables, data types, and operators. It explains how to write and run Python code, use conditional statements, loops, and functions, as well as introduces lists. The document serves as a foundational guide for understanding and using Python effectively.

Uploaded by

ndabenhlevutha84
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)
5 views10 pages

Python Beginner

This document provides an introduction to Python programming for beginners, covering its features, syntax, and basic concepts such as variables, data types, and operators. It explains how to write and run Python code, use conditional statements, loops, and functions, as well as introduces lists. The document serves as a foundational guide for understanding and using Python effectively.

Uploaded by

ndabenhlevutha84
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

PYTHO N NOTES FO R BEGINNERS

(Based on “How to Think Like a Computer Scientist: Learning with Python”)

1. W hat is Python?

Python is a high- level, general- purpose programming language.


That means it’s designed to be easy for humans to read and write, while still
powerful enough for computers to understand.

Features of Python:

Easy to read — code looks almost like English.

Free and open source — anyone can use and share it.

Portable — works on W indows, Mac, and Linux.

Interpreted — runs line by line (you don’t have to compile it).

Has huge libraries — ready- made code for maths, graphics, data, etc.
2. W riting and Running Python Code

You can write Python in:

IDLE (Python’s built- in editor)

O nline editors (like Replit or Jupyter Notebook)

Text editors (like VS Code)

Example:

Print(“Hello, world!”)

This tells the computer to print (display) the message Hello, world!

3. Python Syntax (Rules)

Like English has grammar, Python has syntax rules.

Example rules:

1. Indentation (spaces) matters!


Python uses spaces to show which lines belong together.
If True:
Print(“Indented properly”)

2. Case sensitive — Print and print are not the same.

3. U se # for comments (notes that don’t run).

# This is a comment

4. Variables

A variable is a name used to store data in memory.


Think of it like a box with a label.

Example:

Name = “Fanele”
Age = 19

Name variable that stores “Fanele”


Age variable that stores 19

We can use them later:

Print(“My name is”, name)


Print(“I am”, age, “years old”)

5. Data Types

Python has different kinds of data depending on what you want to store.

TypeExampleDescription

Int10W hole number


Float3.14Decimal number
Str“Hello”Text (string)
BoolTrue or FalseLogic (true/false values)

You can check a variable’s type:

X = 3.14
Print(type(x)) # < class ‘float’>
6. Input from the U ser

You can ask the user to type something:

Name = input(“Enter your name: “)


Print(“Hello,”, name)

input() always gives a string, even if you type numbers.

To change that into a number:

Age = int(input(“Enter your age: “))

7. Basic O perators

You can do maths with Python!

O peratorExampleMeaning

+5 + 2Addition
- 5 –2Subtraction
*5 * 2Multiplication
/5 / 2Division (gives a float)
//5 // 2W hole number division
% 5 % 2Remainder
**5 ** 2Exponent (power)

Example:

A = 10
B = 3
Print(a // b) # 3
Print(a % b) # 1

8. Comparison O perators

U sed to compare two values (gives True or False).

O peratorMeaningExample

==Equal toa == b
!=Not equal toa != b
Greater thana > b
< Less thana < b
> =Greater or equala > = b
< =Less or equala < = b
9. Conditional Statements

To make decisions, we use if, elif, and else.

Example:

Age = int(input(“Enter your age: “))

If age > = 18:


Print(“You are an adult.”)
Elif age > 12:
Print(“You are a teenager.”)
Else:
Print(“You are a child.”)

Indentation (the spaces before print) shows which lines belong to the if.

10. Loops

Loops help you repeat actions.


a) W hile loop — repeats while a condition is True

Count = 1
W hile count < = 5:
Print(count)
Count = count + 1

b) For loop — repeats for each item in a sequence

For i in range(5):
Print(i)

range(5) means numbers 0,1,2,3,4

11. Functions

A function is a block of code that performs a task when called.

Example:

Def greet():
Print(“Hello, Fanele!”)

Greet()
Functions can also take arguments:

Def greet(name):
Print(“Hello,”, name)

Greet(“Fanele”)

And return values:

Def add(a, b):


Return a + b

Result = add(3, 5)


Print(result)

12. Lists (Basic Introduction)

A list stores multiple items in one variable.

Example:

Fruits = [“apple”, “banana”, “cherry”]


Print(fruits[0]) # apple

Lists can change:


[Link](“orange”)
Print(fruits)

13. Summary

ConceptMeaningExample

VariableStores datax = 5
Data typeType of valueint, str, float
InputU ser typingname = input()
If- statementMake decisionsif x > 0:
LoopRepeat codefor i in range(5):
FunctionReusable blockdef greet():

You might also like