create account

Learn Basic Python Programming EP.6. - Arrays & File Operations by profitgenerator

View this thread on: hive.blogpeakd.comecency.com
· @profitgenerator · (edited)
$10.68
Learn Basic Python Programming EP.6. - Arrays & File Operations
<center>![p1.png](https://steemitimages.com/DQmU3VQGGHcbE5JyZBGTF8iN1d1xustawjxytWr4bP5kMNt/p1.png)</center>
<br/>


[Maybe I dived too deeply into advanced stuff the last part](https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-5-let-s-build-a-password-strength-analyzer), but I just wanted to show you how powerful Python is and you can code basically anything with it. Some tools can be extremely useful especially for crypytocurrency holders. You can even build a trading bot for Poloniex and Bittrex in Python, using their API system, it would be a piece of cake. It would be harder to just just make it profitable, but the code itself can be easily made.

Ok but let's go back to novice level and let me teach you more basic things like arrays and file operations. An [array](https://en.wikipedia.org/wiki/Array_programming) is like a variable that has multiple elements but each of them can be indexed. In Python even a string which is supposed to be an unitary variable can be indexed, but an array is specifically for indexing.

* An integer variable is like this: 12345
* An array variable is like this [1,2,3,4,5]  (where each element has an index, the number 5 will have an index of 4 because the count starts always from 0)

An array can be initialized, which means that it's size is specified:
`array_A = [0] *5`

This will create an array of size 5, where each element is filled with 0. Of course the array can later be extended if needed.

Or you can create an empty array and fill it up later:
`array_B = []`

To fill up an array we can use the `for` loop which we learned about in [EP 3](https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-3-logic-and-loops):


```python
for x in range(5):
 array_B.append(x)
```

<BR/>

This will create an array of size 5, and it will fill it up from 0 to 4.

![P1.png](https://steemitimages.com/DQmehQ1LtdTd9WNsvS5hPx8zz9pXAP2cQ3JuYW4SM5txrQ3/P1.png)

<BR/>

![P2.png](https://steemitimages.com/DQmP4YzKiCi3GBi1drKJrqp5mDkk5M2oLYLvjhH29nHn2vy/P2.png)

<BR/>

----------------------

<BR/>

# File Operations

Working with file is also easy. There are 2 basic things, reading from a file and writing to a file. For simplicity we will use a plaintext .txt basic file. It will be a simple file just containing 3 hellos in 3 separate lines, like this:

![h1.png](https://steemitimages.com/DQmbRWpMAkohahCX1DAUMx8VdaEBS27ayFK8PMLyxZPkntR/h1.png)

The name of the file is simply `hello` or you can name it `hello.txt` if that is better:

Now let's read the contents of this file and print it out:

```python
f = open("hello","r")
content = f.read()
f.close()

print (content)
```

<BR/>

`f` is the file variable object, we open it, in the first argument we specify the name `hello` or `hello.txt`, then the method which is:
* `r` for reading
* `w` for writing
* `b` for binary operations

Then the content variable will contain all data, as the `read()` function reads the entire file into memory. This is simple and easy but it might be problematic for big files (100 MB larger) where streamreading is recommended instead of reading everything in the memory.

It's also very important to close the file after finishing using it, otherwise the file can be corrupted by disk flushing and things like that. So this comment just return the content of the file:

![f2.png](https://steemitimages.com/DQmaAr6sqKkLyDjh61GidxZ5g5gQD3XN4MxiixyTKuaktPz/f2.png)

But in this instance, the content variable is just a long string, the entire file is read in like a long string of text. You might want to read it in line-by-line instead and store each line in a separate array element, for that do it like this:

```python
with open("hello","r") as f:
    content = f.readlines()
f.close()
print (content[0])
```

<BR/>

This code will just read in the file line by line, until the end of the file, and here the `content` variable is a string array not just a simple string. So by printing out `content[0]` we print out only the first line:


![h2.png](https://steemitimages.com/DQmSAf9U3QRstkT8cs2PiGszwfRk2neJFp7XuUxfDkzLXBb/h2.png)

If you want to print out the entire file this way you need to iterate through all of it like this:

```python
with open("hello","r") as f:
    content = f.readlines()

f.close()

for x in range(len(content)):
 print (str(x)+" Line Content:" + content[x])
```

<BR/>

Where we specify the length of the array in the range with the `len()` function, remember it's 3 since we have 3 lines. And then print out them line by line and also add some text before it. We show the `x` variable which will count the index of it, which is always -1 of the size of it, since it starts from 0. So the size of the array is 3, but the index of the last element is 2.

![H4.png](https://steemitimages.com/DQmSNXcaq1vfbZoi4JBUD6xBRJmts2T2nvphzmpX4sEMkv6/H4.png)


-------------------------------------------------

That's it, I hope you learned something new, I am telling you these are just the basics, there are many more interesting things you can do with these tools.

<BR/>


---------------------------


***Disclaimer: Code licensed under [MIT license](www.opensource.org/licenses/MIT), use this code at your own risk, I will not be responsible for any losses.***


------------------------------------------

**Sources:**
https://pixabay.com
Python is a trademark of the [Python Software Foundation](https://www.python.org/psf/)



-------------------------------------------


<CENTER><H1>Upvote, ReSteem & <a href="https://steemit.com/@profitgenerator" target='_blank'><img src='https://s4.postimg.org/cfz9b1mnh/bluebutton.png' border='0' alt='bluebutton'/></a></H1>
</CENTER>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorprofitgenerator
permlinklearn-basic-python-programming-ep-6-arrays-and-file-operations
categoryprogramming
json_metadata{"tags":["programming","howto","education","python","tutorial"],"image":["https://steemitimages.com/DQmU3VQGGHcbE5JyZBGTF8iN1d1xustawjxytWr4bP5kMNt/p1.png","https://steemitimages.com/DQmehQ1LtdTd9WNsvS5hPx8zz9pXAP2cQ3JuYW4SM5txrQ3/P1.png","https://steemitimages.com/DQmP4YzKiCi3GBi1drKJrqp5mDkk5M2oLYLvjhH29nHn2vy/P2.png","https://steemitimages.com/DQmbRWpMAkohahCX1DAUMx8VdaEBS27ayFK8PMLyxZPkntR/h1.png","https://steemitimages.com/DQmaAr6sqKkLyDjh61GidxZ5g5gQD3XN4MxiixyTKuaktPz/f2.png","https://steemitimages.com/DQmSAf9U3QRstkT8cs2PiGszwfRk2neJFp7XuUxfDkzLXBb/h2.png","https://steemitimages.com/DQmSNXcaq1vfbZoi4JBUD6xBRJmts2T2nvphzmpX4sEMkv6/H4.png","https://s4.postimg.org/cfz9b1mnh/bluebutton.png"],"links":["https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-5-let-s-build-a-password-strength-analyzer","https://en.wikipedia.org/wiki/Array_programming","https://steemit.com/programming/@profitgenerator/learn-basic-python-programming-ep-3-logic-and-loops","www.opensource.org/licenses/MIT","https://pixabay.com","https://www.python.org/psf/","https://steemit.com/@profitgenerator"],"app":"steemit/0.1","format":"markdown"}
created2017-07-02 21:16:51
last_update2017-07-02 21:20:00
depth0
children8
last_payout2017-07-09 21:16:51
cashout_time1969-12-31 23:59:59
total_payout_value8.536 HBD
curator_payout_value2.139 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,702
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,065,020
net_rshares1,469,236,451,080
author_curate_reward""
vote details (46)
@alexandruionescu ·
Thanks @profitgenerator !!!
👍  
properties (23)
authoralexandruionescu
permlinkre-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170702t212026884z
categoryprogramming
json_metadata{"tags":["programming"],"users":["profitgenerator"],"app":"steemit/0.1"}
created2017-07-02 21:20:27
last_update2017-07-02 21:20:27
depth1
children1
last_payout2017-07-09 21:20:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation4,243,893,540,414
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,065,359
net_rshares591,950,989
author_curate_reward""
vote details (1)
@profitgenerator ·
No problem my pleasure teaching people about python.
properties (22)
authorprofitgenerator
permlinkre-alexandruionescu-re-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170702t212436200z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-02 21:24:45
last_update2017-07-02 21:24:45
depth2
children0
last_payout2017-07-09 21:24:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length52
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,065,770
net_rshares0
@g-power ·
I have been learning python until i stoped.I love python and I will love to continue.Is there any e-book you can recommend for easy understanding?Thanks.
properties (22)
authorg-power
permlinkre-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170703t044941943z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-03 04:49:57
last_update2017-07-03 04:49:57
depth1
children1
last_payout2017-07-10 04:49:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length153
author_reputation387,285,167,103
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,099,871
net_rshares0
@profitgenerator ·
I dont know just watch some youtube videos I am sure there are many tutorials out there in a playlist, just learn from there, easier.
properties (22)
authorprofitgenerator
permlinkre-g-power-re-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170703t120000900z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-03 12:00:21
last_update2017-07-03 12:00:21
depth2
children0
last_payout2017-07-10 12:00:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length133
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,133,249
net_rshares0
@whd ·
I will always think that learning python programming is a bad choice. It offers a lot of syntactic sugar so you do not learn how to solve programming problems
properties (22)
authorwhd
permlinkre-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170702t213550161z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-02 21:35:48
last_update2017-07-02 21:35:48
depth1
children3
last_payout2017-07-09 21:35:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length158
author_reputation17,817,550,622,492
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,066,829
net_rshares0
@profitgenerator · (edited)
But you can interact easily with a Linux machine and that is what matters. People should not use Windows anyway due to security and privacy concerns, so if they arleady use Linux they might was well just learn Python programming.
properties (22)
authorprofitgenerator
permlinkre-whd-re-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170702t213723200z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-02 21:37:39
last_update2017-07-02 21:38:12
depth2
children2
last_payout2017-07-09 21:37:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length229
author_reputation68,549,319,463,075
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,066,990
net_rshares0
@whd ·
Is is hard to use C on Linux machine? I want to show you example of "syntactic sugar" i mentioned for example plaindrome problem can be solved with python in such manner ``str(n) == str(n)[::-1]``
Anyway good tutorial voted up:)
properties (22)
authorwhd
permlinkre-profitgenerator-re-whd-re-profitgenerator-learn-basic-python-programming-ep-6-arrays-and-file-operations-20170702t215037786z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-07-02 21:50:36
last_update2017-07-02 21:50:36
depth3
children1
last_payout2017-07-09 21:50:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length228
author_reputation17,817,550,622,492
root_title"Learn Basic Python Programming EP.6. - Arrays & File Operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id7,067,942
net_rshares0