This shows you the differences between two versions of the page.
programming:python [2025/02/17 23:37] – created - external edit 127.0.0.1 | programming:python [2025/08/10 15:07] (current) – adminent | ||
---|---|---|---|
Line 15: | Line 15: | ||
* Import Requests module into Python | * Import Requests module into Python | ||
* From Python cmd line | * From Python cmd line | ||
- | * < | + | * < |
+ | |||
+ | ====Notes==== | ||
+ | https:// | ||
+ | |||
+ | print(" | ||
+ | |||
+ | |||
+ | x = 1 | ||
+ | if x == 1: | ||
+ | # indented four spaces | ||
+ | print(" | ||
+ | |||
+ | |||
+ | To define an integer: | ||
+ | myint = 7 | ||
+ | print(myint) | ||
+ | |||
+ | To define a floating point number: | ||
+ | myfloat = 7.0 | ||
+ | print(myfloat) | ||
+ | myfloat = float(7) | ||
+ | print(myfloat) | ||
+ | |||
+ | Two ways strings are defined: | ||
+ | mystring = ' | ||
+ | print(mystring) | ||
+ | mystring = " | ||
+ | print(mystring) | ||
+ | |||
+ | An example on why you would use double-quotes: | ||
+ | mystring = " | ||
+ | print(mystring) | ||
+ | |||
+ | one = 1 | ||
+ | two = 2 | ||
+ | three = one + two | ||
+ | print(three) | ||
+ | |||
+ | hello = " | ||
+ | world = " | ||
+ | helloworld = hello + " " + world | ||
+ | print(helloworld) | ||
+ | |||
+ | output: | ||
+ | 3 | ||
+ | hello world | ||
+ | |||
+ | Assignments can be done on more than one variable " | ||
+ | a, b = 3, 4 | ||
+ | print(a, | ||
+ | |||
+ | output: | ||
+ | 3 4 | ||
+ | |||
+ | Mixing operators between numbers and strings is not supported: | ||
+ | # This will not work! | ||
+ | one = 1 | ||
+ | two = 2 | ||
+ | hello = " | ||
+ | |||
+ | print(one + two + hello) | ||
+ | |||
+ | mystring = " | ||
+ | myfloat = 10.0 | ||
+ | myint = 20 | ||
+ | |||
+ | # testing code | ||
+ | if mystring == " | ||
+ | print(" | ||
+ | if isinstance(myfloat, | ||
+ | print(" | ||
+ | if isinstance(myint, | ||
+ | print(" | ||
+ | |||
+ | output: | ||
+ | | ||
+ | Float: 10.000000 | ||
+ | Integer: 20 | ||
+ | |||
+ | 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) | ||
+ | |||
+ | Exercise | ||
+ | In this exercise, you will need to add numbers and strings to the correct lists using the " | ||
+ | |||
+ | You will also have to fill in the variable second_name with the second name in the names list, using the brackets operator []. Note that the index is zero-based, so if you want to access the second item in the list, its index will be 1. | ||
+ | |||
+ | numbers = [1,2,3] | ||
+ | strings = [" | ||
+ | names = [" | ||
+ | |||
+ | # write your code here | ||
+ | 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(" | ||
+ | |||
+ | output: | ||
+ | [1, 2, 3] | ||
+ | [' | ||
+ | The second name on the names list is Eric | ||
+ | |||
+ | Or you can use: | ||
+ | numbers = [] | ||
+ | strings = [] | ||
+ | names = [" | ||
+ | |||
+ | # write your code here | ||
+ | numbers.append(1) | ||
+ | numbers.append(2) | ||
+ | numbers.append(3) | ||
+ | |||
+ | strings.append(" | ||
+ | strings.append(" | ||
+ | |||
+ | 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(" | ||
+ | |||
+ | number = 1 + 2 * 3 / 4.0 | ||
+ | print(number) | ||
+ | |||
+ | output: | ||
+ | 2.5 | ||
+ | |||
+ | remainder = 11 % 3 | ||
+ | print(remainder) | ||
====Backlinks==== | ====Backlinks==== | ||
[[: | [[: |