Skip to Main Content

Python Programming Language

A guide to the fundamentals of programming in the python language, with additional resources.

Data Types

Integer: a number that is positive or negative 1, 2, -7, 17, etc.
Float: ( including double)  a number with a decimal 1.0, 3.2, 3.14159 etc.
Boolean : a value of “True” or “False”

String: text and numbers.  Strings must have single or double quotation marks, e.g. “Hello World”, ‘Hello world’, “7.34”, ‘3.14159’.  Note that when numbers have quotation marks it is a string and not a float or integer.  Note that boolean values are an exception and do not have quotation marks.

List values: These are ordered collections of objects, enclosed in square brackets, such as [1, 2, 3], ["apple", "banana", "cherry"], or [1, "apple", True].

Tuple values: These are ordered collections of objects, enclosed in parentheses, such as (1, 2, 3), ("apple", "banana", "cherry"), or (1, "apple", True).

Set values: These are unordered collections of unique objects, enclosed in curly braces, such as {1, 2, 3}, {"apple", "banana", "cherry"}, or {1, "apple", True}.

Dictionary values: These are collections of key-value pairs, enclosed in curly braces, such as {"name": "Alice", "age": 30}, {"fruit": "apple", "color": "red"}, or {1: "one", 2: "two"}.

Operators

Arithmetic Operators

+ addition

- subtraction

* multiplication

/ division

// floor division

% modulo division

** exponentiation

  • Comparison Operators

== equality (represented by two equal signs)

< less than

> greater than

<= less than or equal to

>= greater than or equal to

!= not equal to

  • Assignment Operators

= assignment

+= in-place addition operator

-= in-place subtraction operator

*= in-place multiplication

/= in-place division operator

           Implementation:

x=3

y=4

print(x+y)

7

a=15

a *=3

print(a)

35

a=15

a+=5

print(a)

20

x=15

x/=3

print(x)

5

Logical Operators

Logical Operators are used to combine conditional states and these often use the comparison operators return the Booleans TRUE of FALSE

AND : Combines two conditions

OR : Disjoins two conditions

NOT : Excludes subsequent condition

Implementation Example:

age = 25
salary = 50000
is_retired = FALSE
age = 67
salary = 30000
is_retired = False
if (age >= 18 and age <=65) and (salary >= 30000 or is_retired == True):
    print("Eligible for a loan")
else:
    print("Not eiligible for a loan")

 

 

Variables

Indentation

Comments

Functions

print(): Used to output text to the console.
len(): Used to return the length of an object, such as a string or list.
type(): Used to return the data type of an object.
int(), float(), str(): Used to convert a value to a specific data type.
range(): Used to generate a sequence of numbers.
input(): Used to get user input from the console.
sorted(): Used to sort a list.
sum(): Used to calculate the sum of a list or other iterable.
max(), min(): Used to return the maximum or minimum value in a list or other iterable.
abs(): Used to return the absolute value of a number.
round(): Used to round a number to a specified number of decimal places.

A more detailed list can be found here:  https://docs.python.org/3/library/functions.html 

Libraries

Statements

Conditions

©2018 Morgan State University | 1700 East Cold Spring Lane Baltimore, Maryland 21251 | 443-885-3333 | Privacy | Accessibility