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')

Operators

+

/ - 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:

  • 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.
price = 25
print(price > 10 and price < 30)
print(price > 10 or price <30)

Comparison Operators

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

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

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.

<

less than

> 

greater than

<= 

less than or equal to

>= 

greater than or equal to

== 

equal to

!= 

not equal to