Python Basics
Python Basics
What is Python:
What is Python?
Python is a programming language: this includes the "library" of data we can use and a
piece of software called an "interpreter" that reads and executes the code we create. The
name Python comes from the surreal British comedy group "Monty Python" due to the
creator of the language, Guido van Rossum, being a fan of the show: He wanted his
language to be "fun" to use.
Download Python:
https://www.python.org/
Python Basics
Python Syntax:
General Naming Conventions
• Python has three standard naming conventions for working with different kinds of
data: Screaming Snake Case, Snake Case, and Pascal Case.
• Screaming Snake Case
o must start with an uppercase letter
o all applicable characters must be upper case
o individual words should be separated by an underscore
o example:
▪ SCREAMING_SNAK3_CASE
• Snake Case
o must start with a lower case letter
o individual words should be separated by an underscore
o example:
▪ snak3_case
• Pascal Case
o must start with an upper case letter
o individual words should be separated by a starting uppercase letter
o example:
▪ PascalCase
Each of these naming conventions is used to represent different kinds of data or meanings
in your code.
• Screaming Snake case is used when working with constant value (think pi) in your
code
• Pascal Case is used when working with classes
• Snake Case is used in all other cases
Formatting Conventions:
Unlike many other languages, Python uses indentation to represent specific interactions
and ownership in code. The main way you will see this play out in your code is how the
code is indented. Consider the following code that saves two names for later use
Python Basics
The code above can be executed fine. However, if adjusted slightly, the code will break
Notice how the second line is indented: this will cause your program to not work. As you
work through the modules make sure you pay attention to the syntax of the examples.
Comments:
Any text after a "#" symbol is ignored by the Python interpreter; this is the simplest way
to create comments. You can wrap your comments in triple single or double quote
characters (' or ") to create multi line comments.
Single Comment
To create a single line comment you simply add a "#" symbol to the line. The comment is
any content after the symbol, so you can write some code before the comment if you
want, but not after.
# This is a comment
name = "Ted" # this is also a comment, the name variable will still be set
Multi-Line Comment
If you need a comment to span multiple lines you can wrap it in triple single or double
quotes.
'''this is a
multi-line
comment'''
OR
"""
you could also
Python Basics
do your comment
this way
"""
Variables:
Named storage locations used to hold data during program execution.
Data Types:
Different types of data that can be stored in variables, such as integers, floats, strings,
booleans, lists, tuples, dictionaries, and more.
Type Conversion:
Python allows implicit and explicit type conversion between different data types using
built-in functions and constructors.
Variable Naming:
Variables in Python should follow naming conventions, such as using descriptive names,
avoiding reserved words, and using lowercase with underscores for readability
(snake_case).
Float Variable:
Python Basics
height = 1.75
String Variable:
name = "John Doe"
Boolean Variable:
is_valid = True
Type Conversion:
Implicit Type Conversion:
num_int = 42
num_float = num_int + 0.5 # Implicitly converts integer to float
• Implicit type conversion occurs when different data types are combined in
operations. Here, num_int is implicitly converted to a float when added to 0.5.
Explicit Type Conversion:
num_str = "123"
num_int = int(num_str) # Explicitly converts string to integer
• Explicit type conversion is done using built-in functions like int(), float(), str(),
etc. Here, num_str is converted from a string to an integer using int().
Python Basics
Variable Naming:
Following naming conventions:
first_name = "Alice" # snake_case for variable names
num_attempts = 5 # descriptive variable names
• Variable names should follow conventions like using lowercase letters and
underscores for readability (snake_case). Descriptive names like first_name and
num_attempts enhance code clarity.
Checking Data Types:
Using the type() function:
num = 42
print(type(num)) # Output: <class 'int'>
The type() function is used to determine the data type of a variable. Here, it confirms that
num is of type integer (int).
• Lists are used to store collections of items. Here, numbers is a list containing
integers from 1 to 5.
Tuples:
point = (10, 20)
• Tuples are similar to lists but are immutable (cannot be changed). point is a tuple
containing coordinates (10, 20).
Dictionaries:
person = {"name": "Alice", "age": 30}
• Dictionaries store key-value pairs. person is a dictionary with keys "name" and
"age", mapping to values "Alice" and 30, respectively.
Python Basics
Sets:
unique_numbers = {1, 2, 3, 4, 5}
Operators:
Arithmetic Operators
+ # addition
- # subtraction
* # multiplication
/ # division
** # power of
% # modulus
// # floor division
Assignment Operators
= # assigns a value to a variable
+= # adds to the existing variable's value
-= # subtracts from the existing variable's value
*= # multiplies the existing variable's value by a value
/= # divides the existing variable's value by a value
%= # returns the modulus of the existing variable value
//= # returns floor division with the existing variable value
**= # raises the existing variable's value to a power
Comparison Operators
!= # checks if values are not equal
== # checks if values are equal
> # checks if first value is greater than second
< # checks if first value is less than second
>= # checks if first value is greater than or equal to second
<= # checks if first value is less than or equal to second
Logical Operators
is # checks if objects share a memory address
is not # checks if objects do not share a memory address
Python Basics
and # this lets you require multiple logical checks to be true in order to continue code
execution
or # this lets you specify multiple logical checks in which only one need be true to
continue code execution
Membership Operators
in # returns True if the object is in a collection
not in # returns True of the object is not in a collection
Print
To display data in the console you use the "print" function
print("Hello world!")
If you want to change the separator between objects you can set a value called "sep" to
your desired separator in the function arguments
print("Hello","world!", sep="-")
Finally, the default behavior for the function is to end the line with a new line character,
which you can change by setting an "end" value in the argument to your desired end
character
Thank you