Skip to content

Commit

Permalink
GH/ID/PL - Realpython, learnpython
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Nov 21, 2019
1 parent ccf127f commit 3c4258b
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 0 deletions.
55 changes: 55 additions & 0 deletions learnpython.org/Basic Operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Check out the documentation on the Basic-Operators.md file in this folder

# Examples of operators
number = 1 + 2 # sets the value of number to 3 (1 + 2) - integer variable
print(number) # prints the value of the variable number, in this case 3

# Modulus operator
remainder = 11 % 3 # sets the value of the variable "remainder" to the remainder of 11 divided by 3
print(remainder)

# Types of operators in Python
# +. Addition
# -. Subtraction
# *. Multiplication
# /. Division
# %. Modulus - returns the remainder of a division
# Power operators
squared = 7 ** 2 # sets the value of "squared" (an integer variable) to the value of 7^2 - 49
cubed = 7 ** 3 # sets the value of "cubed" (an integer/float variable) to the value of 7^3 = 343
print(cubed + squared) # prints the value of cubed added to the value of squared

# Operators & Strings
# addition and strings
helloworld = "hello " + "world" #sets the value of the string variable "helloworld" to "hello + world" = "hello world"
print(helloworld) # prints the value of "helloworld", in this case hello world
# multiplication abd strings
lotsofstring = "string" * 10 # sets the value of "lotsofstring" to 10xstring
print(lotsofstring) # prints string x10

# Operators can be used with lists
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)

print([1,2,3] * 3) # prints "1,2,3" 3 times

# Exercise for "Basic Operators" from LearnPython.org
x = object()
y = object()

# TODO: change this code
x_list = [x,x,x,x,x,x,x,x,x,x]
y_list = [y,y,y,y,y,y,y,y,y,y]
big_list = ([x,y] * 10)

print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))

# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
94 changes: 94 additions & 0 deletions learnpython.org/Basic-Operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# LearnPython.org >> Basic Operators

[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)

* Just as with other programming languages, basic operators (+, -, /, *) can be used in Python

An example of using operators in integer variables;

```python
number = 1 + 2 # sets the value of the int variable "number" to 3
print(number) # prints the value of the variable "number", in this case 3

# note: all the code on these documentation files are also put into Python files in the same folder
```

* There are many different operators:

## Types of Operators

* +. Addition
* -. Subtraction
* *. Multiplication
* /. Division
* %. Modulus - returns the remainder of a division

Modulus example:

```python
remainder = 11 % 3 # sets the value of the variable "remainder" to the remainder of 11 divided by 3
print(remainder)
```

2 multiplication symbols put together create a power relationship:

```python
squared = 7 ** 2 # sets the value of "squared" (an integer variable) to the value of 7^2 - 49
cubed = 7 ** 3 # sets the value of "cubed" (an integer/float variable) to the value of 7^3 = 343
print(cubed + squared) # prints the value of cubed added to the value of squared
```

## Operators & strings

* Python supports ***concatenating*** strings using the ***addition*** operator:

```python
helloworld = "hello " + "world" #sets the value of the string variable "helloworld" to "hello + world" = "hello world"

print(helloworld) # prints the value of "helloworld", in this case hello world
```

* You can also use the multiplication operator with strings:

```python
lotsofstring = "string" * 10 # sets the value of "lotsofstring" to 10xstring
print(lotsofstring) # prints string x10
```

## Operators & Lists

Operators can be used with lists:

```python
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
```

```python
print([1,2,3] * 3) # prints "1,2,3" 3 times
```

## Exercise

```python
x = object()
y = object()

# TODO: change this code
x_list = [x,x,x,x,x,x,x,x,x,x]
y_list = [y,y,y,y,y,y,y,y,y,y]
big_list = ([x,y] * 10)

print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))

# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
```

2 changes: 2 additions & 0 deletions learnpython.org/Hello World!.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("Hello, World!")
# This prints "Hello World" to the console
8 changes: 8 additions & 0 deletions learnpython.org/Learn Python Workspace.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "C:\\Users\\arbuc\\OneDrive\\Documents\\GitHub\\python-learning"
}
],
"settings": {}
}
22 changes: 22 additions & 0 deletions learnpython.org/Lists - LearnPython.org.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Lists - LearnPython.org

* Lists - similar to ***arrays*** (variables which can hold more than one value)
* Lists can contain any type of variable
* They can contain as many variables as you want

A list can be built like this:

```python
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3

# prints out 1,2,3
for x in mylist:
print(x)
```

17 changes: 17 additions & 0 deletions learnpython.org/Python - Hello World.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Python - Hello World

*From LearnPython.org*

https://www.learnpython.org/en/Hello%2C_World%21

* The "print" directive prints out a line
* It also includes a newline (unlike in the C programming language)

A string can be written in Python like this:

```python
print("Hello World")
# This would print out "Hello World" to the console, without the quotation marks
```

The exercise can also be seen in Github/IrisDroidology/Python-Learning, go to the Master Branch, then LearnPython.org, then HelloWorld!.py
62 changes: 62 additions & 0 deletions learnpython.org/Python - Variables & Types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Variables & Types

*From LearnPython.org*

https://www.learnpython.org/en/Variables_and_Types

* Python is **object oriented**
* It is not **statically typed**
* This means you do not need to declare variables before using them
* You also do not need to declare their type

## Types of Variables - Numbers

### Integer

* Integer - whole number

An example of setting up an integer and printing it:

```python
myint = 7 # this sets the value of "myint" to 7
print(myint) # this prints the value of "myint", in this case 7
```

### Floating point number

* Floating point number - number with decimal point

**Can be a whole number!**

```python
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
```



### Strings

* Defined with either double or single quotation marks ('' or "")
* It is better to use "" as you can then use apostrophes in the string

```python
# Example of String variable in Python
mystring = "This is a string"
print(mystring) # prints the variable of "mystring"
```

### Operators

* Operators (+, -, *, /) can be used

Example:

```python
string1 = "This is a string"
string2 = "that is half-complete"
print(string1 + string2) #prints the value of "string1" & "string2", in this case "This is a string""that is half-complete". Note that there is no space between string1 and string2
```

24 changes: 24 additions & 0 deletions learnpython.org/String Formatting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# String Formatting - LearnPython.org

* Python uses ***c-style*** string formatting
* This can create new, **formatted** strings
* The ***%*** operator is used to format a set of variables enclosed in a **tuple** with a **format string**
* Example: '%s' =

```python
# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name) # %s refers to the name variable
```

* %s refers to string variable
* %d refers to number variable

Example:

```python
name = "Liam"
age = 16
print("%s is %d years old" % (name, age))
```

8 changes: 8 additions & 0 deletions learnpython.org/String Formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name) # %s refers to the name variable (take not of where the exclamation mark is placed)

# %d, %s
name = "Liam"
age = 16
print("%s is %d years old" % (name, age))
Binary file added learnpython.org/img/Hello, World! Exercise.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions learnpython.org/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# LearnPython.org >> Lists

# Example list - from the LearnPython site
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3

# prints out 1,2,3
for x in mylist:
print(x)

# list error
print(mylist[10]) # this returns an error, as there are only 3 lists (1,2,3) defined right now. There is no list with the value of 10

# List exercise
numbers = []
numbers.append(1)
numbers.append(2)
numbers.append(3)
strings = []
strings.append("hello")
strings.append("world")
names = ["John", "Eric", "Jessica"]

# write your code here
second_name = []


# this code should write out the filled arrays and the second name in the names list (Eric).
print(numbers)
print(strings)
print("The second name on the names list is %s" % second_name)


# Solution to exercise
numbers = []
strings = []
names = ["John", "Eric", "Jessica"]

# write your code here
numbers.append(1)
numbers.append(2)
numbers.append(3)

strings.append("hello")
strings.append("world")

second_name = names[1]

# this code should write out the filled arrays and the second name in the names list (Eric).
print(numbers)
print(strings)
print("The second name on the names list is %s" % second_name)

0 comments on commit 3c4258b

Please sign in to comment.