Sololearn - Python
---------------------
1. Write Code
------------
Python is one of the most popular and useful languages used to give instructions to
computers
The print() instruction is the easiest way to send a message to the screen or other
display device.
print("Level Up!")
The print() instruction requires the use of parentheses around the message.
Make sure you use quotation marks around the text messages.
It all starts with a line of code.
Numbers don't require quotation marks.
2. Memory & Variables
-------------------
Computer programs use variables to remember important information, like items in a
shopping cart, prices and discounts.
The line of code below tells the computer to store information in a variable called
item
item = "bike"
To create a variable, you just need to give it a name.
The information that you need to store is added on the right.
Variables have a name and a value. They are connected with the equal sign =.
Computer programs can store information like the user's progress in a videogame
with...Variables
🌟 Computer programs use variables to remember important information
🌟 A variable has a name and a value
🌟 You can create a variable by connecting the name and the value with an equal sign
=
3. Text Data
---------
A large amount of information out there consists of text. A piece of text data is
called a string.
Strings in Python need to be surrounded by quotation marks.
We use quotation marks to tell Python that we are working with a piece of text
data.
Strings can be stored in variables.
In Python, both single ' and double " quotes can be used to define strings. It
doesn’t make a difference whether you choose double or single quotation marks.
A computer program is made of lines of code. You can add as many lines and
variables to your code as you need to give the computer instructions.
The code in computer programs is made of statements. Statements are the
instructions for the computer to follow. Real programs can contain thousands of
statements.
The print() statement is the easiest way to send a value to the screen.
🌟 A piece of text is called a string
🌟 Strings require quotation marks
🌟 The print() statement is used to send a value to the screen
4. Numerical Data
----------------
Numerical data is information that comes in the form of numbers.
Numerical values can be directly stored in variables.
You can send a number to the screen with the print() statement. You just need to
insert the number between the parentheses.
You can perform math operations with numbers. Each print() instruction will add a
value to the screen in a new line.
You can use the print() statement to check that the computer is following your
instructions.
A variable’s name is used to identify where that information is stored. You can
access the value that a variable is storing by calling its name.
🌟 Numerical values can be stored in variables
🌟 You can access the value stored in a variable by calling its name
🌟 Numerical data should not be surrounded by quotation marks
5. Working with Variables
-----------------------
Variables are key to software development. They allow you to store, label and play
with data.
You can store the result of a calculation in a variable.
You can create a new variable to store the result of a calculation made using other
variables.
You can update the value stored in a variable. The variable will forget the
previously stored value.
Updating the value of a variable is called reassigning a variable.
🌟 You can run calculations using the values stored in variables
🌟 You can store the result of a calculation in a variable
🌟 Updating the value of a variable is called reassigning a variable
Debugging (Module 2)
=============================
Standards and Best Practices... Applying Best Practices
---------------------------------------------------------
Coding consists of 3 steps:
- Writing
- Executing (or running)
1 message = "Debugging
2 print(message)
File "./Playground/file0.py", line 1
message = "Debugging
^
SyntaxError: EOL while scanning string literal
The computer reads and executes instructions line by line, from top to bottom. The
execution of the program will be interrupted at the first error encountered.
Calling the name of a variable that has not been defined is a very common error.
🌟 Errors in code are known as bugs
🌟 Code is executed line by line from top to bottom
🌟 Code execution is interrupted by bugs
You can add comments to your Python code with the hash symbol #
Comments are not instructions, they are ignored by machines.
You can use comments to temporarily disable a statement. This way the computer will
skip the instruction.
Python is a case-sensitive language, meaning "A" and "a" are treated as different.
🌟 You can add comments to your code with the hash symbol #
🌟 Python is a case-sensitive language
🌟 Snake case is the best practice when creating multi-word variable names
Spaces are not allowed in variable names. Python will return an error if your
variable names contain spaces.
A variable name can contain numbers but cannot start with a number.
🌟 Spaces are not allowed in variable names
🌟 A variable name cannot start with a number
🌟 Best practices can help you avoid errors
2. Inputs and Outputs
-----------------------
Computer programs are designed to interact with users and the outside world.
In this lesson, you'll learn to create code that takes information in and sends
information out.
An input is any information that goes into a computer.
The press of a key and the click of a button are examples of inputs.
The input() instruction is the easiest way to allow a user to insert a value into
your program.
An output is a way for the computer to communicate with the outside world. A
message displayed on the screen and the sound from a speaker are examples of
outputs.
The print() instruction, which you already know, is the easiest way to get your
computer program to generate an output.
🌟 inputs and outputs help machines communicate with the outside world
🌟 the input() instruction allows the user to enter a value into your program
🌟 the print() instruction is used to generate an output
3. Data Types
---------------
Data comes in different shapes and forms. Computers treat different types of data
in different ways.
String is the data type for a piece of text. The quotation marks tell the computer
that a value needs to be stored as a string.
Computers store and process different types of numbers. Integers are whole numbers
without a decimal point. They can be positive, negative or zero.
Float is the data type for numbers with decimal places, they can be positive or
negative.
The way computers operate with values depends on the data type.
When you use the + addition operator with string values the two strings are joined
together.
This is known as concatenation
Anything in quotation marks will be treated as a string, even numbers.
You won't be able to do math operations if numbers are surrounded by quotes. They
will be treated as strings.
🌟 computers store and process different data types differently
🌟 string is the data type for text
🌟 integer and float are data types for numbers
Working With Data (Module 3)
=============================
1.Data Type Checking
----------------------
Data comes in a variety of shapes and forms. Dealing with data in the incorrect
format can result in data loss or corruption.
In this lesson, you'll learn to check the data type stored in a variable.
Data can come to you in the incorrect format. You can use the type() instruction to
check the data type stored in a variable.
balance = "780"
type(balance)
It's a String
---
city = "Berlin" #stores a string
age = 42 #stores an integer
balance = 830.29 #stores a float
print(type(city)) #outputs <class 'str'>
print(type(age)) #outputs <class 'int'>
print(type(balance)) #outputs <class 'float'>
output
-----
<class 'str'>
<class 'int'>
<class 'float'>
Why do we use data types?
To tell a computer how to store and behave with a value
- The division of two integers always produces a float.
a = 4
b = 2
c = 4/2
print(c) #displays the result
print(type(c)) #displays data type
output
------
2.0
<class 'float'>
🌟 the type() instruction is used to check the data type
🌟 the division of two integers always produces a float
2. Data Conversion
-------------------
Data can come in the incorrect format. Data from surveys and web forms can come to
you with quality issues.
In this lesson, you'll learn to convert data to get it into the correct format.
The input() instruction always turns the user input into a string, no matter what
the user enters.
birth_year=input() #takes an input
print(type(birth_year)) #displays the data type
output
------
<class 'str'>
You can convert data from one type to another to fix data quality issues.
The int() instruction converts any type of value into an integer
x = "55" # x is a string
print(type(x))
y = int(x) # y is an integer
print(type(y))
output
------
<class 'str'>
<class 'int'>
You can use the int() instruction to convert the user input into an integer.
# User input is converted into an integer
height = int(input())
print(type(height))
# The line above is an effective way to combine 2 instructions into one
# height1 = input()
# height2 = int(height1)
output
------
<class 'int'>
- There are situations when you need values to be treated as floats.
The float() instruction converts values into floats.
In a similar way, you can ensure that values are converted into strings with the
str() instruction.
The int(), str() and float() instructions are examples of explicit conversion,
which means they are performed by an instruction given by a programmer (like you).
On the other hand, run the code to see some examples of implicit (automatic) data
type conversions
# Examples of automatic data type conversion
x = 5 # integer
y = 2 # integer
z = x/y # float (implicit conversion)
print(z)
a = 3 # integer
b = 1.5 # float
c = a + b # float
print(c)
output
------
2.5
4.5
🌟 you can change the data type of a value with int(), float() and str()
🌟 there are implicit and explicit data type conversions in Python
🌟 str(), int(), float() instructions are explicit conversions
The execution of your program can fail if your data is in the incorrect format.
In this lesson, you’ll put your data type conversion skills into practice to fix
data quality issues.
the code to take two numbers and add them together
score1 = int(input())
score2 = int(input())
total_score = score1 + score2
- The str() command can help you with concatenations.
- Math operations between integers and floats produce a float.
🌟 you can use explicit data type conversions to avoid bugs in your programs
🌟 int() ensures that the user input is treated as an integer number
🌟 str() can help you concatenate numbers with text
3. Comparison Operations
--------------------------
Computers are faster and more precise than humans at certain operations.
In this lesson, you'll learn about a type of operation that makes machines evaluate
different scenarios and make decisions.
A comparison operation always results in either one of these two outcomes: Yes or
No
print(30 < 25)
print(5 < 9)
print(50 > 100)
output
-----
False
True
False
The result of a comparison operation in Python is either True or False.
Computers use binary code to represent information. By turning switches ON and OFF,
we change the information stored in a computer.
- What does binary mean in this context?
-->two possibilities for the state of a switch
You are now ready to meet another data type. The Boolean is a data type that only
has two possible values: True or False.
print(type(5 < 9))
print(type(50 > 100))
print(type(True))
print(type(False))
output
------
<class 'bool'>
<class 'bool'>
<class 'bool'>
<class 'bool'>
Comparison operations and Boolean values allow machines to make decisions.
🌟 the Boolean data type has one of two possible values: True or False
🌟 a comparison operation always results in a Boolean
4. Logical Operations
-----------------------
Modern computers can perform complex tasks very fast because these can be broken
down into lots of tiny, simple calculations.
In this lesson, you’ll learn about another type of operation that computers can do
faster than humans.
Logical operations are needed for machines to evaluate complex scenarios.
Logical operations use Boolean values. Do you remember what a Boolean is?
A data type with two possible values: True or False
- The "and" operation results in a True value only when all the inputs are True at
the same time. What’s the result of this "and" logical operation?
A logical operation combines Boolean inputs to produce a Boolean output.
True and True = True
Every other combination = False
- The "or" logical operation results in a True value if at least one of the inputs
is True. What’s the result of this "or" logical operation?
Logical operations need to be inserted in print() instructions for the result to be
outputted.
print(True and False)
print(False and True)
print(True or False)
print(False or True)
output
------
False
False
True
True
🌟 logical operations take multiple Boolean values as input
🌟 logical operations produce a single Boolean value as output
🌟 "and" and "or" are examples of logical operations
5. Combining Comparison and Logical Operations
-----------------------------------------------
In this lesson, you’ll mix comparison and logical operators to make even fast, more
accurate programs
- You can store boolean values in variables just like you do with other data types.
You can store the result of a comparison operation as a variable.
Python is a case-sensitive language.
- Both "True" and "False" start with an uppercase letter.
- Both "and" and "or" operators are lowercase in Python.
- You can put parentheses around the operations that should be done first. It makes
the code easier to read.
a = (3 > 2) or False
output
-----
True
🌟 You can store boolean values in variables
🌟 You can store the result of logical and comparison operations in variables
🌟 You can combine operations with logical and comparison operators
Control Flow (Module 4)
===========================
1. Control Flow
2. For Loops
3. While Loops
4. More on Iteration
5. Conditional Statements
6. More on Conditional Statements
Working with Lists (Module 5)
==============================
1. Lists
2. Indexing
3. Using Indexing
4. Slicing
5. Using Slicing
6. Advanced Slicing and Indexing
Functions (Module 6)
======================
1. Functions
2. Function Arguments
3. String Functions
4. List Functions
5. Custom Functions
6. More on Custom Functions
Collection Types (Module 7)
==============================
1. Dictionaries
2. Dictonary Functions
3. Tupes
4. Tuple Unpacking
5. Sets
6. List Comprehension
Functional Programming (Module 8)
==================================
1. Introduction
2. Lambdas
3. map
4. Generators
5. Decorators
6. Recursion
7. *args and **kwargs
OOP (Module 9)
================
1. Classes
2. Inheritance
3. Magic Methods & Operator Overloading
4. Data Hiding
5. Class & Static Methods
6. Properties
Exceptions (Module 10)
=========================
1. Exceptions
2. Exception Handling
3. finally, else
4. Raising Exceptions
Working with Files (Module 11)
=================================
1. Opening Files
2. Reading Files
3. Writing Files
4. Working with Files