Site Tools


programming:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

programming:python [2025/02/17 23:37] – created - external edit 127.0.0.1programming: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
-      * <code>import requests</code>+      * <code>import requests</code>\\  
 + 
 +====Notes==== 
 +https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/prog/configuration/167/b_167_programmability_cg/python_api.html 
 + 
 +print("This line will be printed."
 + 
 + 
 +x = 1 
 +if x == 1: 
 +    # indented four spaces 
 +    print("x is 1.") 
 + 
 + 
 +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 = 'hello' 
 + print(mystring) 
 + mystring = "hello" 
 + print(mystring) 
 + 
 +An example on why you would use double-quotes: 
 + mystring = "Don't worry about apostrophes" 
 + print(mystring) 
 +  
 +one = 1 
 +two = 2 
 +three = one + two 
 +print(three) 
 + 
 +hello = "hello" 
 +world = "world" 
 +helloworld = hello + " " + world 
 +print(helloworld) 
 + 
 +output:  
 +
 +hello world 
 + 
 +Assignments can be done on more than one variable "simultaneously" on the same line like this: 
 +a, b = 3, 4 
 +print(a,b) 
 + 
 +output: 
 +3 4 
 + 
 +Mixing operators between numbers and strings is not supported: 
 +# This will not work! 
 +one = 1 
 +two = 2 
 +hello = "hello" 
 + 
 +print(one + two + hello) 
 + 
 +mystring = "hello" 
 +myfloat = 10.0 
 +myint = 20 
 + 
 +# testing code 
 +if mystring == "hello": 
 +    print("String: %s" % mystring) 
 +if isinstance(myfloat, float) and myfloat == 10.0: 
 +    print("Float: %f" % myfloat) 
 +if isinstance(myint, int) and myint == 20: 
 +    print("Integer: %d" % myint) 
 + 
 +output: 
 + String: hello 
 +    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 "append" list method. You must add the numbers 1,2, and 3 to the "numbers" list, and the words 'hello' and 'world' to the strings variable. 
 + 
 +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 = ["hello","world"
 +names = ["John", "Eric", "Jessica"
 + 
 +# 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("The second name on the names list is %s" % second_name) 
 + 
 +output: 
 +    [1, 2, 3] 
 +    ['hello', 'world'
 +    The second name on the names list is Eric 
 + 
 +Or you can use: 
 +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) 
 + 
 +number = 1 + 2 * 3 / 4.0 
 +print(number) 
 + 
 +output: 
 +2.5 
 + 
 +remainder = 11 % 3 
 +print(remainder) 
  
 ====Backlinks==== ====Backlinks====
 [[:programming|Programming]]\\  [[:programming|Programming]]\\ 
programming/python.1739835436.txt.gz · Last modified: by 127.0.0.1