Skip to main content

Python Fundamentals

Python Command Index

print print string ("") or variable value ().
variable = value Set a variable. space and comma seperated.

input prompt user input

data types and conversion

integer (int)- whole number

floating point (float) - decimal

boolean (bool) - true or false

string (str) - string

Python notes

name = input ("What is your name? ")
age = input ("How old are you? ")
new_patient = input ("Are you a new patient? ")
print(name, age, new_patient)

Type Conversion

birth_year = input ("Enter your birth year ")
age = 2024 - int(birth_year)
print (age)

datatype(variable_to_convert)+

Basic number + number

first_number = float(input("First: ")) ##variable is set as type float, and takes input from user
second_number = float(input("Second: ")) ##variable is set as type float, and takes input from user
total = (first_number) + (second_number) ##variable is set as the sum of first + second variables
print ("Sum " + str(total)) ##String Sum is printed, as well as the variable total. Python cannot concatenate different variable types.

variable options/methods

(variable.find('string') Search for a string. Prints index of finding
.upper/.lower

upper and lower case

.replace('oldstring', 'newstring')
math.ceil(number) Round a number up to the nearest whole number
math.floor(number) Rounds a number down to the nearest whole number
abs(number) Absolute Value Function - swaps positive/negative numbers
pow(number, number)

Operators

+ addition.
* Multiplication. You can multiply an int by a string to produce that amount of that string.
/ Floating point division
// 

integer division

%

Modulus Operator



Mathematics

+addition
-subtraction
*multiplication
/Division
//

Floor Division

an operation that divides two numbers and returns the largest integer less than or equal to the quotient.

 

When you divide 7 by 3, you get approximately 2.3333. The floor division 7 // 3 returns 2 because 2 is the largest integer that is less than or equal to 2.3333.

%

Modulus operator

The modulus operation returns the remainder after dividing one number by another.

**

Exponentiation

Exponentiation raises one number (the base) to the power of another number (the exponent).

2 ** 3 means 2 raised to the power of 3, which equals 2 * 2 * 2 = 8.

+

/ - division floating point.

// - division integer

% - Modulus operator

Example

If you have 7 % 3, the operation would be:

  • 7 divided by 3 equals 2 with a remainder of 1.
  • So, 7 % 3 equals 1.

** - exponent operator

to the power of

10 ** 2 = 10²


Logical Operators

Logical operators in Python are used to combine conditional statements. They allow you to perform logical operations on expressions and return a boolean value (True or False). Python has three logical operators:

price = 25
print(price > 10 and price < 30)
print(price > 10 or price <30)

and

Returns True if both statements are true.

or

Returns True if one of the statements is true.

not

Reverses the result, returns False if the result is true.

Comparison Operators

These operators compare two values and return a boolean result (True or False).

price = 25
print(price > 10 and price < 30)

<

less than

> 

greater than

<= 

less than or equal to

>= 

greater than or equal to

== 

equal to

!= 

not equal to


IF Statements

An if statement in Python is a control flow statement that allows you to execute a block of code only if a specified condition is true.

temperature = 35
if temperature > 30:
    print ("It's a hot day")

ELSE IF Statements

The elif statement in Python, short for "else if," is used to check multiple conditions in an if statement. It allows you to specify additional conditions if the previous conditions are not true. There's no limit on the number of elif conditions you can add.

temperature = 35
if temperature > 30:
    print ("It's a hot day")
elif temperature > 20:
    print("It's a nice day")

ELSE Statements

The else statement in Python is used in conjunction with if and elif statements to provide a block of code that executes when none of the preceding conditions are true. It acts as a default case for your conditional logic, ensuring that there's always a defined action if none of the specified conditions are met.

temperature = 35
if temperature > 30:
    print ("It's a hot day")
elif temperature > 20:
    print("It's a nice day")
else:
    print("It's probably cold")

WHILE Loops

A while loop in Python is used to repeatedly execute a block of code as long as a specified condition is true. It is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute until the condition becomes false.

a = 1
while a <= 10:
    print(a)
    a = a + 1

LISTS

Lists in Python are ordered collections of items that can hold a variety of object types, including other lists. They are one of the most versatile and commonly used data types in Python, allowing you to store, modify, and manipulate sequences of elements.

names = ["John", "Bob", "Dan"] #Create a LIST of names
names[0] = "jon" # Change index 0 in the LIST
print(names[0]) #Print index position 0 from the LIST
print(names[0:3]) #Print the first 3 index (names) from the LIST
numbers = [1, 2, 3, 4, 5] # Creates a list of numbers 1-5
numbers.append(6) #Append number 6 to the end of the list
numbers.insert(6, 7) #Add number 7 to index position 6
print(1 in numbers)#Check for the number 1 in the LIST numbers. Boolean