Docsity

Introduction to Python Programming: Core Concepts and Syntax, Lecture notes of Computer Science

Partial preview of the text

Download Introduction to Python Programming: Core Concepts and Syntax and more Lecture notes Computer Science in PDF only on Docsity!

CS 2280

Intro to Programming

(Python)

Based on notes by Kyle Burke

A program is a sequence of instructions that specifies how to perform a computation. Core instructions include:

Input: Get data from the keyboard, a file, or other device. Output: Display data on the screen, save to a file, etc. Math: Perform basic mathematical operations like addition and multiplication. Conditional Execution: Check for certain conditions and run the appropriate code. Repetition: Perform some action repeatedly, usually with some variation.

The Way of the Program

A value is a basic piece of data (e.g., a number or text). A type is its category.

You can check a value's type with the type() function:

The value 'Hello' is a str (string).

The value 42 is an int (integer). The value 3.14 is a float (floating-point number).

Values and Data Types

A variable is a name that refers to a value. Think of it as a labeled box where you can store data.

You create a variable with an assignment statement :

Variables

A function is a named sequence of statements that performs a specific task. You "call" it to run it.

Built-in Functions: Python provides many, like type() and print().

Function Call: The syntax for running a function.

Argument: The value(s) you pass into the function. (e.g., 42).

Return Value: The result the function passes back. (e.g., ).

What is a Function?

Use the def keyword to create a new function. The indented block of code is the function's body.

A parameter (like message) is a variable that receives the argument when the function is called.

Output:

Defining Your Own Functions

Loops allow you to repeat code. A for loop is a simple way to repeat code a specific number of times.

The range(3) function generates a sequence of numbers (0, 1, 2). The loop runs once for each number.

Repetition: for Loops

Recursion

A recursive function is a function that calls itself. It's a powerful way to

solve complex problems by breaking them into smaller, identical

sub-problems.

A recursive function must have two parts:

1 .

Base Case: A simple condition that stops the recursion (to prevent an infinite loop). 2 .

Recursive Case: The part of the function that calls itself, moving closer to the base case.

https://files.realpython.com/media/Basic-Data-Types-in-Python_Watermarked.e3dd34457952.jpg Source: realpython.com

https://i.sstatic.net/FCmOT.png Source: stackoverflow.com

https://d8it4huxumps7.cloudfront.net/uploads/images/66582881cd196_python_functions_1.jpg?d=2000x Source: unstop.com

https://www.edureka.co/blog/wp-content/uploads/2020/01/Blank-Diagram-10.png Source: www.edureka.co

https://craftofcoding.wordpress.com/wp-content/uploads/2021/06/nestingdolls.png?w= Source: craftofcoding.wordpress.com

https://png.pngtree.com/thumb_back/fh260/background/20221015/pngtree-abstract-programming-workflow-on-a-screen-with-developing-real-python-code-photo-image_ .jpg Source: pngtree.com

Image Sources