Docsity

Introduction to Python Programming: A Beginner's Guide with Turtle Graphics, Summaries of Computer science

Toggle sidebar
bg1
1. Introduction to Python
What is Python?
Python is a high-level, interpreted, and versatile programming
language known for its clear syntax and ease of learning.
First Steps with Python
Python uses ā€œscriptsā€ to execute commands, and a Python script
typically ends in .py
Code Example: Hello World
# This is a comment - it is not executed by Python
# Prints a message on the screen
print("Hello World!")
2. Python Basics
Variables in Python
• A variable is like a box where you can store information.
• Example: number = 5 stores the value 5 in the variable number
Using input() to Enter Data
• The input() function allows the user to enter data.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Introduction to Python Programming: A Beginner's Guide with Turtle Graphics and more Summaries Computer science in PDF only on Docsity!

1. Introduction to Python

What is Python?

Python is a high-level, interpreted, and versatile programming

language known for its clear syntax and ease of learning.

First Steps with Python

Python uses ā€œscriptsā€ to execute commands, and a Python script

typically ends in .py

Code Example: Hello World

This is a comment - it is not executed by Python

Prints a message on the screen

print("Hello World!")

2. Python Basics

Variables in Python

• A variable is like a box where you can store information.

• Example: number = 5 stores the value 5 in the variable number

Using input() to Enter Data

• The input() function allows the user to enter data.

• Example: name = input("What is your name? ")

Code Example : Personalized Greeting

Asks for the user's name

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

Displays a greeting

print("Hello, " + name + "!")

3. Conditional Structures

Imagine you have a robot that follows instructions you give it.

Sometimes, you want the robot to do something if a condition is

true, and something else if the condition is false. This is

where if and else come in Python!

Using if-else in Python :

The if-else structure allows for decision-making based on conditions.

Example: Execute a block of code if a condition is true.

Code Example: Even or Odd

Asks for a number from the user

number = int(input("Enter a number: "))

Checks if the number is even or odd

if number % 2 == 0 : print("The number is even.") else: print("The number is odd.")

4. Loops in Python

Check if a Number is Even

Program to check if a number is even

number = int(input("Enter a number: ")) if number % 2 == 0 : print("The number is even.") else: print("The number is odd.")

Addition of Two Numbers

Program to add two numbers

number1 = float(input("Enter the first number: ")) number2 = float(input("Enter the second number: ")) result = number1 + number print("The sum of the two numbers is:", result)

Calculate the Average of Three Numbers

Program to calculate the average of three numbers

number1 = float(input("Enter the first number: ")) number2 = float(input("Enter the second number: ")) number3 = float(input("Enter the third number: ")) average = (number1 + number2 + number3) / 3 print("The average of the three numbers is:", average)

Check if a Number is Positive, Negative, or Zero

Program to check if a number is positive, negative, or zero

number = float(input("Enter a number: ")) if number > 0 : print("The number is positive.") elif number < 0 : print("The number is negative.") else: print("The number is zero.")

6. Introduction to Turtle

What is Turtle?

• Turtle is a built-in graphical library in Python, used to

create simple drawings and animations.

Configuring Turtle

• Turtle is included in the standard Python installation. To

use it, simply import it into your script.

Code Example : Your First Drawing

import turtle

Creates a new turtle object

t = turtle.Turtle()

Moves the turtle to draw

t.forward( 100 ) # Moves forward 100 pixels t.left( 90 ) # Turns 90 degrees t.forward( 100 )

Ends the drawing

turtle.done()

7. Drawing with Turtle

Basic Commands

• forward(x): moves forward x pixels.

• backward(x): moves backward x pixels.

• right(angle): turns right by angle degrees.

• left(angle): turns left by angle degrees.

turtle.end_fill() # Ends the color filling once the polygon is complete

Initialization of the Turtle environment

window = turtle.Screen() # Creates a new Turtle window for drawing turtle.speed( 3 ) # Sets the turtle's drawing speed

Example of using the polygon function

polygon("blue", 5 , 100 ) # Uses the function to draw a blue pentagon window.mainloop() # Keeps the window open to display the drawing

In this program, the polygon function is defined to create a polygon

according to given specifications. You can call this function with

different

values to create various polygons. For example, polygon("red", 4, 80)

would draw a red square.

8. Movement Commands

Movement commands allow moving the turtle and creating various

shapes.

• forward(distance) or fd(distance): Moves the turtle forward by

ā€˜distance’ units.

• backward(distance) or bk(distance): Moves the turtle backward

by ā€˜distance’ units.

• right(angle) or rt(angle): Turns the turtle right by ā€˜angle’

degrees.

• left(angle) or lt(angle): Turns the turtle left by ā€˜angle’ degrees.

• goto(x, y): Moves the turtle to the specified position.

• setx(x): Moves the turtle to the x-coordinate.

• sety(y): Moves the turtle to the y-coordinate.

• circle(radius): Draws a circle with the specified radius.

• dot(size=None, color=None): Draws a dot.

9. Control Commands

These commands include the turtle’s movement speed and pen

control.

• speed(speed): Sets the movement speed of the turtle (1–10).

• penup() or pu(): Lifts the pen, no drawing when the turtle

moves.

• pendown() or pd(): Puts down the pen, draws when the turtle

moves.

10. Pen and Color Commands

These commands change the pen color, and start and end the filling

of shapes.

• color(color): Changes the pen color.

• begin_fill(): Begins to fill the drawn shape.

• end_fill(): Ends the filling of the shape.

• fillcolor(color): Changes the fill color.

11. Window Commands

These commands control the Turtle window interface, such as the

title, background color, and clear or reset drawings.

programming for middle and high school students. This pedagogical

tool makes learning programming accessible and engaging, thanks

to an intuitive user interface and a range of features suited for

beginners.

Key Features of EduPython :

• Simplified Interface : EduPython offers a streamlined

and easy-to-navigate interface, allowing students to focus

on the essentials: coding and understanding programming

concepts.

• Integrated Pedagogical Resources : The IDE includes

resources and code examples to help students get started

and understand the fundamentals of Python.

• Compatibility with School Curricula : EduPython is

designed to align with school curriculums, making its

integration into classrooms smooth and effective.

EduPython is thus an ideal choice for educators looking to introduce

Python programming in their courses in an interactive and

structured manner.

Download Links for the Latest Version EP3.1 :

• Installable Version: Download the latest version: 3.

• Compressed Version: zip version