-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5721cf
commit a43ea7e
Showing
28 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Casting (Changing the data type) | ||
|
||
## Strings & Integer Variables | ||
|
||
```python | ||
number1 = "5" # string variable | ||
number2 = 10 # integer variable | ||
``` | ||
|
||
Right now, we've got two (2) variables set: | ||
|
||
* Number1 (String) | Value = "5" | ||
|
||
* Number2 (Integer) | Value = 10 | ||
|
||
### Error 1 | ||
|
||
If I just added these 2 variables together: | ||
|
||
```py | ||
number3 = number1 + number2 # results in a syntax error | ||
``` | ||
|
||
#### Fixing error 1 | ||
|
||
```python | ||
number3 = int(number1) + number2 | ||
``` | ||
|
||
By adding "int(" to the variable, we change the data type of variable "number1" from a **string** into an **integer.** | ||
|
||
```python | ||
print(number3) # prints "15" | ||
``` | ||
|
||
### Adding 2 strings together | ||
|
||
```python | ||
number3 = number1 + str(number2) # prints "510" as 10 is added to the right of the 5, like how you would add two characters/letters together, as both variables are now strings | ||
``` | ||
|
||
### Error 2 | ||
|
||
```python | ||
number1 = "ggg" | ||
number2 = 5 | ||
number3 = number2 + number1 | ||
``` | ||
|
||
Even if I was to convert "number1" into a string, there would still be an error: | ||
|
||
```python | ||
number3 = int(number1) + number2 | ||
``` | ||
|
||
And this is because "ggg" is not a number, so it is an invalid value for any integer or float variable. | ||
|
||
However, we can add these two variables together: | ||
|
||
##### Fixing Error 2 | ||
|
||
```python | ||
number3 = number1 + str(number2) # and this gives us "ggg5" // See "adding two strings together" | ||
``` | ||
|
||
### Floats | ||
|
||
```python | ||
number1 = 11.55 | ||
number2 = 3 # could be represented as a float or an integer, but to make sure it's an integer: | ||
|
||
number2 = int(number2) | ||
|
||
number3 = number1 + number2 | ||
``` | ||
|
||
```python | ||
number3 = float(number2) + number1 | ||
``` | ||
|
||
The same rules regarding converting strings/integers also apply to floats | ||
|
||
#### Types of errors | ||
|
||
* Syntax | ||
|
||
* Type | ||
|
||
### Other stuff | ||
|
||
* Changing a variable's data type to the current data type does not result in an error and the program continues to run | ||
|
||
## Full code | ||
|
||
```py | ||
number1 = "5" # string variable | ||
number2 = 10 # integer variable | ||
|
||
# number3 = number1 + number2 // Error 1 | ||
|
||
number3 = int(number1) + number2 # Fixing Error 1 | ||
print(number3) | ||
|
||
|
||
number1 = "ggg" | ||
number2 = 5 | ||
|
||
#number3 = number1 + number2 | ||
|
||
|
||
number3 = number1 + str(number2) | ||
print(number3) | ||
|
||
|
||
number1 = 11.55 | ||
number2 = 3 # could be represented as a float or an integer, but to make sure it's an integer: | ||
|
||
number2 = int(number2) | ||
|
||
number3 = number1 + number2 | ||
``` | ||
|
||
## File Information | ||
|
||
* File Link: https://github.com/irisdroidology/python-learning/master/udemy/python-masterclass/11.casting.md | ||
|
||
* Discussion link: http://acord.software/stellarios/python-discussion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Strings | ||
|
||
These are both strings: | ||
|
||
```python | ||
"string" | ||
'string' | ||
``` | ||
|
||
|
||
|
||
```python | ||
stringVar = "Hello" # string variable aka | ||
``` | ||
|
||
## Printing parts of a string | ||
|
||
```python | ||
print(stringVar) # prints whole string | ||
print(stringVar[0]) # index value - starting from 0 // Prints "H" | ||
print(stringVar[1]) ## prints "e" | ||
``` | ||
|
||
* Indexing is how we can print parts of a string (as well as doing other things) | ||
|
||
* Index values start from 0 - the first value of a variable is the "0th" or where the index value is 0 | ||
|
||
|
||
|
||
An error: | ||
|
||
```python | ||
print(stringVar[5]) # index error | ||
``` | ||
|
||
* While there are 5 characters in the string "stringVar", if the index value is 5, then it is referring to the **6th** character, which doesn't exist, thus resulting in an index error: | ||
|
||
* ``` | ||
string index out of range | ||
``` | ||
|
||
* |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.