View on GitHub

reads

https://yazansneneh.github.io/reads/

Beginner Python Course

A Python editor

Strings and numbers

Variables

-Variable names can contain underscores (_), numbers, and letters. They cannot start with a number.

Output and Input

comments in python.

Lists in python.

print(friends[0][1]) # 24 print(friends[1][0]) # Bob


##### add things to a list?
- append(the thing)
- friends.append("Jen")
-  you can .append() anything you want—since a list can contain any type of element.
-  example:
```python
friends = ["Rolf", "Bob", "Anne"]
friends.append(["Hello", "World"])

print(friends)  # ["Rolf", "Bob", "Anne", ["Hello", "World"]]

remove things from a list?

print(friends) # [“Rolf”, “Anne”]

- **Note:**  if you have a list of lists, you'll need to tell .remove() everything the exact thing you want to delete.

#### copy a list?
- **.copy()**
- in python if i want to copy list from another list i would naturally assing it like so listCopy = listOriginal.
  - because arrays are storing by reference if i change on listCopy, i will also update on original one.
- however, to solve this i use **.copy()** method.
```python
friends = ["Rolf", "Bob", "Anne"]

listCopy = listOriginal.copy()
friends_abroad.remove("Bob")

Lists in lists

If statements

if statement in python

learning_programming = True

if learning_programming:
    print("You're awesome!")

The if statement is comprised of four parts:

  1. The if keyword, first of all.
  2. The conditional, which must evaluate to True for the body to run.
  3. A colon ( : )
  4. In the next line and indented, the body of the if statement.

INFO: What is indentation?

compound if statement

if condition_vlue: do somrthing… elif condition_Value: do something… elif condition_Value: do something.. elif condition_Value: do something… else: default value evaluate to all other remaining conditions.


### Truthyness and Falsyness in Python
- **bool()** function take any expression and convert it to boolean.
- almost all values can be true, for example:
  -  bool('string ') evaluate to true.
  - bool(1) evaluate to true.
  - bool(true) eval to true.
  - bool([1,2,3]) eval to true.
- values can be false are the following:
  - bool('') false: empty body strings.
  - bool(0), bool(0.0) eval to false.
  - bool(null) eval to false.
  - bool([]) eval to false.

#### not.
- make the condition evaluate to the opposite of the boolean value, for example if i have true condition appened to not, it will become false.
- in if condition statement, in order to negate or use and, or i do following:
  - if condition and condition:
  - if condition or condition:


### Dictionaries
**- What is a dictionary?**
  - dictionary is pretty much list but it store values in key pair value.
  - each key is unique in dictionary.
  - in order to create a dictionary i define dictionary name, then = sign then curly braces and key between qutation and : then value.
  - i seperate each pair by comma.
  - example: 
```python
dictionary = {
  'key':value1,
  'key2': value2
}
Accessing a value stored in a dictionary

Adding multiple items at once

dict1.update(dict2)

- I can add iteratable to dictionary that happened to be key pair value.
- list is iterable, I know it because i use for loop and while loop on it.
  - example:
```python
dict1 = {
  'instructor':'hanaa'
}
dict1.update([['student':'Yazan'], ['student2':'Rama']])

friends_abroad = friend_names_to_info

friends_abroad[“Bob”] = {“name”: “Bob Sponge”, “age”: 20}


#### Use cases for dictionaries
- The main purpose of a dictionary is to allow you to easily access the values (in case of the example above, the ages) given you only know the keys (the names of friends).
- if you know the friend name you can easily find out their age by using that dictionary.
- A dictionary that maps user IDs to their details;
- A dictionary that maps blog authors to their blog addresses.
- A dictionary that maps coffee bean names to coffee bean information.
### Working with Keys and Values
#### Dictionaries and loops
- We can use the keys individually to get each value, but when we're doing a repeated action, it's generally a good idea to use a loop.
- to return keys by looping on dictionary:
```python
for student in student_attendance:
    print(student)
# output is going to be keys

Checking for dictionary membership

Common pitfalls

print() print(“Bye, world!”) # Error


#### Reusing names (unwittingly)
```python
friends = ["Rolf", "Bob"]

def add_friend():
    friend_name = input("Enter your friend name: ")
    friends = friends + [friend_name]  # Another way of adding to a list!

add_friend()
print(friends)  # Always ['Rolf', 'Bob']

def add_friend(): friend_name = input(“Enter your friend name: “) global friends friends = friends + [friend_name] # Another way of adding to a list!

add_friend() print(friends) # Could be [‘Rolf’, ‘Bob’, ‘Anne’]

- or the otherway is, python can see variables outside of the function, so the better way to use it is like this
```python
friends = ["Rolf", "Bob"]

def add_friend():
    friend_name = input("Enter your friend name: ")
    friends.append(friend_name)

add_friend()
print(friends)  # Could be ['Rolf', 'Bob', 'Anne']

Using a function before defining it

The variable name, _, is commonly used when we need to create a variable (the for loop expects one) but we don’t actually want to use it.

for _ in range(2):
  print('a dick')

arguments

add(x=2, y=3) # 5 ```