π Python Basic#
This guide introduces Python programming fundamentals in a structured, interactive way. Youβll learn how to run Python programs, use variables, perform basic calculations, write functions, and control program flow using conditional statements and loops. The goal is to give you hands-on experience with Python and teach you the concepts that serve as the foundation of modern programming.
Note
The only way to learn a new programming language is by writing programs in it. - Dennis Ritchie (inventor of C programming language)
Running Python#
Running Python in VS Code#
To open the
ece487_wksp
folder in VS Code, right-click on it and chooseOpen with Code
from the menu.To open the
Terminal
window, go to View and click onTerminal
.Type in
python
and pressEnter
This will launch the Python Shell, where you can input code and see the output. The Shell acts as a sandbox, allowing you to try out code snippets and get immediate feedback. For example, in the Shell, type
2+3
and press Enter, and it will display5
on the next line.Open a folder: Right-click on the
master_ws
folder and chooseOpen with Code
from the context menu.Open the terminal: Go to the View menu and click on Terminal to open an integrated terminal window in VS Code.
Launch Python Shell: Type
python
and pressEnter
to start the Python interactive shell.Interactive Python: The Python Shell allows you to write and test code interactively. For example, type
2+3
and pressEnter
. It will display the result5
on the next line.Try the following code snippets in the Python Shell:
2 + 3 # Addition of two numbers "hello" + " world" # String concatenation a = 2 # Assign a value to a variable print(a) # Print the variable value
The Python Shell can function as a calculator:
2 + 2 # Basic addition 2 + 2.0 # add an integer to a floating point number 2 * 3 # 2 times 3 2 ** 3 # 2 cubed 2 ** 128 # Python supports very large integers 16 / 3 # = 5.3333, Division returns a floating-point result 16 // 3 # = 5, Integer division 2 % 3 # Modulo (remainder of division)
Exit the Shell: To leave the Python Shell, type
exit()
and pressEnter
, or use the shortcutCtrl+Z
followed byEnter
.
Running Python in Command Prompt#
Open Command Prompt or PowerShell (on Windows) or Terminal (on Mac or Linux).
Run
python
on Windows orpython3
on Mac/Linux to start the interactive Python Shell.Use the Shell exactly as described above to try out Python code.
Python Programming Basics#
Variables in Python#
Python variables can store different types of data, such as numbers, text, or boolean values.
Some common Python data types include:
int
(Integer): Whole numbers, e.g.,42
float
(Floating point): Numbers with decimals, e.g.,3.14
str
(String): Text, e.g.,"ECE 387"
bool
(Boolean): True/False valuesNoneType
: Represents the absence of a value, e.g.,None
Examples of Variables#
# Integer variable
x = 10
print(type(x)) # Outputs: <class 'int'>
# Float variable
pi = 3.14159
print(type(pi)) # Outputs: <class 'float'>
# String variable
name = "Stan Baek"
print(type(name)) # Outputs: <class 'str'>
# Boolean variable
is_active = True
print(type(is_active)) # Outputs: <class 'bool'>
# NoneType variable
data = None
print(type(data)) # Outputs: <class 'NoneType'>
String Concatenation and Casting#
# String concatenation
first_name = "Stan"
last_name = "Baek"
full_name = first_name + " " + last_name
print(full_name) # Outputs: Stan Baek
# Combining strings with numbers
age = 726
# This raises an error: print("Age: " + age)
# Fix by converting age to a string
print("Age: " + str(age)) # Outputs: Age: 726
Math Operators in Python#
Basic operators:
Addition:
+
Subtraction:
-
Multiplication:
*
Division:
/
Advanced operators:
Exponentiation:
**
(e.g.,2**3
gives8
)Modulo:
%
(e.g.,5 % 2
gives1
)Integer Division:
//
(e.g.,7 // 2
gives3
)
Logic and Comparison Operators#
Python supports logical operators for making comparisons and combining conditions:
Operator |
Meaning |
Example |
---|---|---|
|
Equal to |
|
|
Not equal to |
|
|
Greater than |
|
|
Less than |
|
|
Greater than or equal |
|
|
Less than or equal |
|
|
Logical AND |
|
|
Logical OR |
|
|
Logical NOT |
|
Example:#
x = 6
y = 7
print(x == 6 and y == 7) # Outputs: True
print(x == 6 or y == 6) # Outputs: True
print(not (x == 6 and y == 6)) # Outputs: True
Working with Strings#
In Python, strings are sequences of characters enclosed in quotes, like
"Stan Baek is the legendary pirate captain."
Numbers can be integers (e.g.,42
) or decimals (e.g.,3.14
).Try the following code in the Python console:
# A string variable name = "Stan Baek"
To check the type of a variable, use the
type()
function:type(name)
This will return
<class 'str'>
, indicating that the variablename
is a string.Add another string variable:
title = "the legendary pirate captain"
Strings can be combined (concatenated) using the
+
operator:character = name + ": " + title print(character) # Outputs: Stan Baek: the legendary pirate captain
If you try to combine a string with a number directly, Python will raise an error:
# Integer variable age = 726 # This will cause a TypeError print(character + age)
To fix the error, convert the number to a string using the
str()
function:print(character + " is " + str(age) + " years old.")
This will output:
Stan Baek: the legendary pirate captain is 726 years old.
You can also ask for user input with the
input()
function:age = input("How old are you? ") print(type(age)) # By default, input is a string # Convert the input to an integer age = int(age) print(f"You are {age} years old!")
When creating string variables, you can use either single quotes (
'
) or double quotes ("
). However, be consistent in your code to avoid confusion.
Functions and Modules#
Defining Functions#
def add(a, b):
"""Returns the sum of two numbers."""
return a + b
print(add(5, 7)) # Outputs: 12
Using Modules#
# Import the math module
import math
print(math.sqrt(16)) # Outputs: 4.0
print(math.pi) # Outputs: 3.141592653589793
Python Modules#
You can add extra functionality to Python by importing modules. Modules are collections of additional functions built by others. For example, to access advanced mathematical functions, you can import the
math
module:import math print(math.sqrt(16)) # Outputs 4.0 print(math.sin(math.pi/2)) # Outputs 1.0 print(math.cos(0)) # Outputs 1.0
In Python, itβs a convention to import all required modules at the top of the script. This ensures all dependencies are loaded before theyβre used and makes the code easier to understand. You can also import specific functions from a module:
from math import sqrt, sin, pi print(sqrt(16)) print(sin(pi/2))
We can also import individual functions from a module
from math import sqrt, sin, pi print(sqrt(16)) print(sin(pi/2)) cos(0) # error because cos was not imported
You can also use an alias for a module:
import math as m m.sqrt(16) m.sin(m.pi/2)
Conditional Statements#
The
if
,else
, andelif
statements control the flow of execution based on conditions. The basic syntax is:if expression: statements elif expression: statements else: statements
If no action is needed for a certain condition, you can use the
pass
statement:if expression: pass # Do nothing. else: statements
Example:
word = input("Enter a four-letter word: ") if len(word) == 4: print(word, "is a four-letter word. Well done.") elif len(word) == 3: print(word, "is a three-letter word.") else: print(word, "is not a four-letter word.")
Loops#
Letβs start with a simple
while
statement.x = 1 while x < 5: print(x) x = x + 1
We can use
break
to exit a loop:while True: print(x) x -= 1 if x == 0: break
The
for
loop is commonly used in Python:for i in [0, 1, 2, 3, 4]: print(i)
We can also use
range()
:for i in range(5): # Loops through 0 to 4 print(i)
Online Tutorials#
Python Style Guide#
Summary#
This guide introduces you to the basics of Python programming:
Running Python in VS Code or the command line
Using variables, operators, and logic
Writing and calling functions
Using built-in and external modules
Controlling program flow with conditionals and loops
Practice writing your own code to build confidence with Python. Remember, the best way to learn programming is through hands-on experience. Happy coding! π