Python Fundamentals
Python Command Index
| 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 % 3equals 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")