Comp Sci Python Basics
Basics of Python
Python covers essential programming elements: Data Types, Variables, Printing, Math, and Comments.Page 1: Overview of Python BasicsFundamental concepts of Python programming are established, focusing on its versatility and ease of use.
Page 2: Accessing Python EnvironmentUse repl.it for coding and executing Python scripts online. Users can create an account to save their work and run Python scripts directly in the browser. Key features include saving projects, running code instantly, and accessing a console for output display.
Page 3: Data Types in PythonPython has five primary data types:
Numbers: Can be whole numbers (integers) or decimals (floats).
Strings: A string is a sequence of characters combined to convey meaning (e.g., 'Hello').
Lists: A list is an ordered collection of items that can include various data types (e.g., [1, 2, 3]).
Booleans: Used to represent two values: True or False, often utilized in conditional statements.
Dictionaries: A dictionary stores pairs of keys (which can be strings or integers) and values, allowing for fast retrieval of data (e.g., {'name': 'John'}).
Page 4: Examples of Data Types
Numbers:
Integers: 0, 1, -5, 45
Floats: 1.0, -0.999, 3.14
Booleans: True, False
Strings: 'Hello, World!', 'Python'
Lists:
Mixed types: [1, 4, -3], ['Sam', 'Alice']
Dictionaries:
Example: {1: 'Matt', 'cola': 'drink'}
Page 5: Creating Variables
Defining Variables:
Syntax:
variable_name = valueExamples:
height = 5width = 2.8first_name = 'John'color_code = '13'
Python auto-detects the data type based on the value assigned.
Page 6: Printing Output
Printing in Python (version 2.7):
Syntax:
printExample code:
print 'Hello, World!'print height
Page 7: Basic Math Operations
Operations allowed: Addition, Subtraction, Multiplication, Exponentiation.
Example of Operations:
print 3 + 2(Addition)print 2.8 - 9.0(Subtraction)print 2.1 * 4(Multiplication)
Page 8: More Math Examples
Continued basic math operations:
For floats, if any number involved is a float, the result will also be a float:
Example operations:
car = 5print car + 2(Addition)
Page 9: Division & Modulo Operations
Regular Division:
The result depends on the types of numbers (integer vs. float).
Example code:
num1 = 13 / 5num2 = 13.0 / 5
Integer division provides the quotient, while float division gives a decimal.
Page 10: Modulo Division
Modulo Operator (%) provides the remainder from division:
Example:
num1 = 11 % 5
Output examples for processing both integer and float inputs.
Page 11: Floor Division
Floor Division (//):
This operator rounds down to the nearest whole number:
Regular example:
num1 = 23.0 / 5.3results in 4.33Floor example:
num2 = 23.0 // 5.3, which results in 4.
Page 12: Order of Operations
Order of operations (PEMDAS):
Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
Example expression:
100 - 25 * 3 % 4
Page 13: Redefining Variables
Python allows for redefining variables.
Example:
apple = 5apple = 9Only the last definition counts.
Page 14: Example of Variable Redefinition
Example demonstrating total population being redefined as new people are added (incremental update):
total = start + new_people_1
Page 15: Comments in Python
Commenting:
Use
#for single-line comments.Multi-line comments can be encapsulated in triple quotes:
''' This is a comment '''
Python will ignore everything after
#on that line, while print statements will be executed.