Skip to content

Commit a5d1e3c

Browse files
KranthiKranthi
authored andcommitted
Updating Readme and other examples
1 parent 9a1c091 commit a5d1e3c

File tree

13 files changed

+93
-6
lines changed

13 files changed

+93
-6
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Byte-compiled / optimized / DLL files
1+
# Byte-compiled / optimized / DLL files-examples
22
__pycache__/
33
*.py[cod]
44
*$py.class
@@ -34,7 +34,7 @@ share/python-wheels/
3434
MANIFEST
3535

3636
# PyInstaller
37-
# Usually these files are written by a python script from a template
37+
# Usually these files-examples are written by a python script from a template
3838
# before PyInstaller builds the exe, so as to inject date/other infos into it.
3939
*.manifest
4040
*.spec
@@ -104,7 +104,7 @@ __pypackages__/
104104
celerybeat-schedule
105105
celerybeat.pid
106106

107-
# SageMath parsed files
107+
# SageMath parsed files-examples
108108
*.sage.py
109109

110110
# Environments
@@ -133,3 +133,4 @@ dmypy.json
133133

134134
# Pyre type checker
135135
.pyre/
136+
main.py

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,37 @@
22
This repository covers python basic foundations with examples and helps to understand the nuts and bolts of the language.
33

44
### The examples are based on 3.8.2 version
5+
6+
# How to execute a python file from bash
7+
python ./name_of_python_file.py
8+
9+
## More detailed info on several different ways to execute the python from bash
10+
* https://stackoverflow.com/questions/4377109/shell-script-execute-a-python-program-from-within-a-shell-script
11+
12+
## Why do we need a virtual environment and how to create virtual environment
13+
###### If you want your code to run in it's own virtual environment without depending on the OS virtual env or on other python interpreters then in those scenarios you need a virtual environment.
14+
* python -m venv new_virt_env
15+
### Activate virtual environment
16+
* source new_virt_env/bin/activate ( In Mac as /bin folder doesn't exists in windows)
17+
* new_virt_env/Scripts/activate ( In windows - Just run this path to activate the virtual environment)
18+
### Deactivate virtual environment and Destroy virtual environment
19+
* deactivate
20+
* rm -rf new_virt_env/ ( Clean up all the files in the folder)
21+
22+
###### Pip installing and uninstalling new packages
23+
* pip install selenium --user ( pass --user if pip install selenium fails)
24+
* pip uninstall selenium
25+
* pip uninstall selenium urllib3 ( to uninstall multiple packages)
26+
27+
###### Upgrading pip
28+
Upgrade pip if any of the package doesn't get installed in the virtual env
29+
30+
* python -m pip install --upgrade pip
31+
32+
###### Other pip commands
33+
* pip --help
34+
* pip freeze ( Outputs the installed packages using pip)
35+
* pip freeze > requirements.txt
36+
* pip install -r requirements.txt ( Install required packages)
37+
38+

examples/BuiltInFunctions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
# String format of Integer..
1919
i = int('457')
2020
print('{}'.format('hello in integer:'), i + 2)
21+
22+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
print('Welcome to python')
2+
a = '35'
3+
b = "45.26"
4+
c = float(b) * float(a)
5+
print('{}{}'.format('c is ', c))

examples/basics/python_executable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python
2+
print("hello wlcome to python !!")

examples/dictionaries/nest_dictionaries.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
for name, languages in fav_lang.items():
88
for langs in languages:
9-
if len(langs) > 1:
9+
#print('lang::{}and length:{}'.format(langs, len(langs)))
10+
if len(languages) > 1:
1011
print("{} favourite languages are:{} and length is::{}".format(name, langs, len(languages)))
1112
else:
1213
print("{} favourite lang is:{}".format(name, languages))
File renamed without changes.

examples/files-examples/file3.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Count the number of lines in a file and printing lines.
3+
4+
fd = open('../../files/text.file', 'r')
5+
count = 0
6+
for line in fd:
7+
count = count +1
8+
print('{}{}'.format('line from file is::', line))
9+
print('{}{}'.format('no of lines::', count))
File renamed without changes.
File renamed without changes.

examples/files-examples/read_lines.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fp = open('../../files/text.file', 'r')
2+
lines = fp.readlines()
3+
for line in lines:
4+
print('{}{}'.format('lines are::', line.strip()))
5+
fp.close()
6+
7+
# Get 5 characters from a line
8+
fp = open('../../files/text.file', 'r')
9+
line = fp.readline()
10+
typeis = type(line)
11+
12+
print('{}{}'.format('stripped line:', line[:5]))
13+
14+
# Get only 2 lines in the file.
15+
fp = open('../../files/text.file', 'r')
16+
line = fp.readlines()
17+
for each_line in line[:2]:
18+
print('{}{}'.format('print each line:', each_line))
19+
20+
fp.close()
21+
22+
#
23+
fp = open('../../files/text.file', 'r')
24+
#line = fp.readline(2)
25+
#print('same line:', line)
26+
#same line: Welcome to python and this is the second
27+
print('same line:', fp.read(2))
28+
#print('file chars length:', len(line))

examples/functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ def invokehellofunction():
33
if int(x) > 100:
44
print("this is a hello function !!")
55
else:
6-
print("x is not greater than 100")
6+
str = "hello"
7+
print(str + " x is not greater than 100")
78
return "hello is returned !!"
89

910

1011
functionReturValue = invokehellofunction()
1112
print("{} {}".format("return value from function::", functionReturValue))
13+
14+
print("does this print ever :::")

files/text.file

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Hello this is a text file - first line
2-
Welcome to python and this is the second line
2+
Welcome to python and this is the second line
3+
Welcome to python and this is the Third Line
4+
Welcome to python and this is the Fourth Line

0 commit comments

Comments
 (0)