- Details
- Category: Example
- Hits: 1500
Hello World
{module Programming}
{source}
<!-- You can place html anywhere within the source tags -->
<pre
class
=
"brush:php;"
>
<?php
$example
= range(0, 9);
foreach
(
$example
as
$value
)
{
echo
$value
;
}
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: Example
- Hits: 14636
Python 3.4
{article Python Content}{title} {text}{/article}
Examples Python 3.4
- Details
- Category: Example
- Hits: 1614
{article Examples__Python 3.4 }{title} {text}{/article}
NameError: name 'raw_input' is not defined
In python 3.x, there is only one function to get user inputs and that is called input
, which is equivalent to Python 2.7's raw_input
.
input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> fname = input('Enter your name: ')
Enter your name: Bill
>>> print('Hello', '!')
Hello !
>>> print('Hello', fname, '!')
Hello Bill !
>>>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> fname = input('Enter your name: ')
Enter your name: Bill
>>> print('Hello', '!')
Hello !
>>> print('Hello', fname, '!')
Hello Bill !
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
>>> num1 = int(input('Enter a number: '))
Enter a number: 2
>>> num2 = int(input('Enter a number: '))
Enter a number: 2
>>> print(num1 + num2)
4
>>>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
>>> num1 = int(input('Enter a number: '))
Enter a number: 2
>>> num2 = int(input('Enter a number: '))
Enter a number: 2
>>> print(num1 + num2)
4
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: Example
- Hits: 1400
{article Examples__Python 3.4 }{title} {text}{/article}
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
class range(stop)
class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.
For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.
Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.
class range(stop)
class range(start, stop[, step])
class range(stop)
>>> for i in range(5):
print(i)
0
1
2
3
4
class range(start, stop)
>>> range(5, 10)
[5, 6, 7, 8, 9]
class range(start, stop[, step])
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
print(i, a[i])
(0, 'Mary')
(1, 'had')
(2, 'a')
(3, 'little')
(4, 'lamb')
>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5))
[0, 1, 2, 3, 4]
>>>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
class range(stop)
>>> for i in range(5):
print(i)
0
1
2
3
4
class range(start, stop)
>>> range(5, 10)
[5, 6, 7, 8, 9]
class range(start, stop[, step])
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
print(i, a[i])
(0, 'Mary')
(1, 'had')
(2, 'a')
(3, 'little')
(4, 'lamb')
>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5))
[0, 1, 2, 3, 4]
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: Example
- Hits: 1290
{article Examples__Python 3.4 }{title} {text}{/article}
The print statement has been replaced with a print() function,
print()
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print()
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: Example
- Hits: 1278
{article Examples__Python 3.4 }{title} {text}{/article}
type a prompt
exit ()
or you can press ctrl+d
- Details
- Category: Example
- Hits: 1425
{article Examples__Python 3.4 }{title} {text}{/article}
Removing the space at the end of its arguments
You can also customize the separator between items, e.g.:
ride = "Airplane"
print("I like to ride in a ", ride, '!');
To avoid that space you will need to explicitly specify the 'sep' keyword.
print ("I like to ride in a ", ride, "!", sep="") # Note the explicit sep=""
output = "".join(["I like to ride in a ",ride,"!"])
print(output)
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ride = "Airplane"
>>> print("I like to ride in a ", ride, '!'); #To avoid that space you will need to explicitly specify the 'sep' keyword.
I like to ride in a Airplane !
>>> print ("I like to ride in a ", ride, "!", sep="") # Note the explicit sep=""
I like to ride in a Airplane!
>>> output = "".join(["I like to ride in a ",ride,"!"])
>>> print(output)
I like to ride in a Airplane!
>>> print ("Note NO space between Airplane!")
Note NO space between Airplane!
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: Example
- Hits: 1354
{article Examples__Python 3.4 }{title} {text}{/article}
Modules
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:
A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes, and variables. A module can also include runnable code.
Modules Names
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
- Details
- Category: Example
- Hits: 1336
{article Examples__Python 3.4 }{title} {text}{/article}
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
- Details
- Category: Example
- Hits: 1363
{article Examples__Python 3.4 }{title} {text}{/article}
Introspection is code looking at other modules and functions in memory as objects, information about them and manipulating them.
- Details
- Category: Example
- Hits: 1259
{article Examples__Python 3.4 }{title} {text}{/article}