How to print two numbers from the same list in Python 2.7

So i was assigned to make a short programm for skill development on lists. Here's the code:

x=[21,23,25,27]
y=[5,6,7,8]
z=x+y
print (z)
z[0]=45
print (z)
a=[x+y]
print a
print (a[1][2])

What we are asked to do is to guess what will the code print.But the last line doesnt work and i always get errors.That line was given straight from the given exercise.The only way to show the [1] and [2] element is to do it in a for i in range command am i correct?I have tried other syntaxes that dont work and i believe that this is the only way.


a=[x+y], here + will create a new list and add it inside another list so to create 2D list with two inner list, use , which act as separator where as + act as a concatenation operation so use , as

a=[x,y]

To print elements with same index using single indexing with new list, we can create a 2D list with keeping same index values together as

x=[21,23,25,27]
y=[5,6,7,8]
z=x+y
print (z)
z[0]=45
print (z)
a = [[i,j] for i,j in zip(x,y)]
# i is element in x and j is element in y
# [i,j] a new list with elements i and j
print(a)
#a=[x,y]
print (a[0]) # [21,5]

short, a=[x+y] , here + will create a new list and add it inside another list so to create 2D list with two inner list, use , which act as separator where as + act as a  Python provides myriad ways to output information. In fact, the number of ways would amaze you. Real-world printing can become complex, so you need to know a few additional printing techniques to get you started. Using these techniques is actually a lot easier if you play with them as you go along. Type the following …


You could use a for statement like you said. But there's also another way. You could try this:

print(a[:3])

The above code will basically print the elements in a from start till 2, i.e., before 3.

List in Python : lists, range, slice, append, len, etc in Python, Output. a is a list here (empty list). [ ] represents a list. Python 2; Python 3 It is not necessary in Python for the list to contain only the same type of elements. range → It gives us a sequence of numbers in between the two integers given to it. In Python, the + operator can only concertante strings as Python is a strongly typed programming language. In order to solve this problem and to achieve our goal to concatenate strings with int we can use these below methods: Python code to concatenate a string with an int a = 5 print ("the value of a is "+str(a)) Output:


The error is down to the index a[1].

example:

x=[21,23,25,27]
y=[5,6,7,8]
z=x+y
print (z)
z[0]=45
print (z)
# a = list of lists, where x+y is the first and only list [[x+y],...]
a=[x+y]
print(a)
# print [x+y]
print (a[0])
# a[1][2] here 1 was out of bounds
# a[0][2] the third member of list [0], i.e. [x+y]
print (a[0][2])
# list of lists, with two memmbers [x, y]
a = [z,y]
print(a)
# first member of x
print(a[0][0])
# print first member of y
print(a[1][0])

output:

# print (z)
[21, 23, 25, 27, 5, 6, 7, 8]
# print(z)
[45, 23, 25, 27, 5, 6, 7, 8]
# print(a)
[[21, 23, 25, 27, 5, 6, 7, 8]]
# print(a[0])
[21, 23, 25, 27, 5, 6, 7, 8]
# print(a[0][2])
25
# print([z,y])
[[45, 23, 25, 27, 5, 6, 7, 8], [5, 6, 7, 8]]
# print a[0][0]
45
# print a[1][0]
5

Python lists - working with lists in Python, A list can contain the same value multiple times. #!/usr/bin/env python # simple.​py nums = [1, 2, 3, 4, 5] print(nums[0]) print(nums[-1]) print(nums) It contains numbers, a boolean value, another list, a string, a tuple, a custom  Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.


there is either a mistake in the question you've been given or in your writing it in here, as a doesn't have an index 1.

a has one element in it, which is a list. that list is the same as z

z = x+y

makes a new list with all x and y elements

a = [x+y]

a is the same as [z] the z list inside a list

inside a is only a[0] which is the list of x+y

you could have a[0][2] which is 25

but there is no a[1]

Lists, The values in list are called elements or sometimes items. The syntax for accessing the elements of a list is the same as for accessing the characters of a string---the a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print c [1, 2, 3, 4, 5, 6] In this example, Python only created one string object, and both a and b refer to it. Let’s jump in by looking at a few real-life examples of printing in Python. By the end of this section, you’ll know every possible way of calling print(). Or, in programmer lingo, you’d say you’ll be familiar with the function signature. Calling print() The simplest example of using Python print() requires just a few keystrokes: >>> >>>


How to print two integer variables in the same line in Python, I have written below that how you can print two integer variables in same line. Use , to separate strings How do I write a program that adds big integers like 2^​100? 1,561 Views How can I put a variable into an array in Python? 2,505 Views. Generally people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print () function by default ends with newline. Python has a predefined format if you use print (a_variable) then it will go to next line automatically.


Print Single and Multiple variable in Python, Consider below two Python code snippets in Python 2.x. “print variable” prints the variables without any brackets '()' and splitted by a space (Produces same output). # Code 1: print ( 1 ). # Output: 1. # Code 2 : print (( 1 )) Python | Convert a list of multiple integers into a single integer · Protected variable in Python  Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.


Print lists in Python (4 Different Ways), Python program to print list. # print the list by converting a list of. # integers to string using map. a = [ 1 , 2 , 3 , 4 , 5 ]. print ( ' ' .join( map ( str , a))). print "in new line  For objects which don’t have a particular representation for human consumption, str() will return the same value as repr(). Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings and floating point numbers, in particular, have two distinct representations. Some examples: