public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Common sense: When you want to print a string, just print it.
>>> print "Hello, world."
#! /usr/bin/env python
print "Hello, world."
$ python hello.py
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"
So, while abc is a valid identifier, 1abc is not :)
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
In Python, we don't declare what kind of data we want to put in a variable.
>>> a = 12
>>> b = 23
>>> a + b
>>> 35
>>> a, b = 10, 12
>>> print a
10
>>> print b
12
# Integer
>>> a = 10
# Float
>>> b = 2.35
# String
>>> c = "Hello"
# List
>>> d = [1, 2, 'blah', 3.2]
# Dictionary
>>> f = {'a': 1, 'b': 2.3, 'c': "foo"}
>>> f['b']
2.3
>>> 2 + 3 5 >>> 23 - 3 20 >>> 22.0 / 12 1.8333333333333333
>>> 14 % 3
2
>>> 4.0 // 3 1.0 >>> 4.0 / 3 1.3333333333333333
>>> 1 and 4
4
>>> 1 or 4
1
>>> -1 or 4
-1
>>> 0 or 4
4
>>> a = 12
>>> a += 13
>>> a
25
>>> a /= 3
>>> a
8
>>> a += (26 * 32)
>>> a
840
a = 234 * (45 - 56.0 / 34)
(6 + 3) - 12 / 3 + 3 * 2 -1
9 - 12 / 3 + 3 * 2 -1
9 - 4 + 3 * 2 - 1
9 - 4 + 6 - 1
5 + 6 - 1
11 - 1
10
>>> a = 8.126768
>>> str(a)
'8.126768'
if expr1:
do this
elif expr2:
do that
...
...
...
else:
do whatever
def functionname(params):
statement1
statement2
>>> def sum(a, b):
... return a + b
>>> res = sum(234234, 34453546464)
>>> res
34453780698L
#!/usr/bin/env python
def change(b):
a = 90
print a
a = 9
print "Before the function call ", a
print "inside change function",
change(a)
print "After the function call ", a
$ ./local.py
Before the function call 9
inside change function 90
After the function call 9
#!/usr/bin/env python
def change(b):
global a
a = 90
print a
a = 9
print "Before the function call ", a
print "inside change function",
change(a)
print "After the function call ", a
>>> def test(a , b=-99):
... if a > b:
... return True
... else:
... return False
>>> test(12, 23)
False
>>> test(12)
True
>>> def func(a, b=5, c=10):
... print 'a is', a, 'and b is', b, 'and c is', c
...
>>> func(12, 24)
a is 12 and b is 24 and c is 10
>>> func(12, c = 24)
a is 12 and b is 5 and c is 24
>>> func(b=12, c = 24, a = -1)
a is -1 and b is 12 and c is 24
>>> def func(a, b=13, v):
... print a, b, v
...
>>> a = [23, 45, 1, -3434, 43624356, 234]
# Append
>>> a.append(45)
>>> a
[23, 45, 1, -3434, 43624356, 234, 45]
# Insert
>>> a.insert(0, 1) # 1 added at the 0th position of the list
>>> a
[1, 23, 45, 1, -3434, 43624356, 234, 45]
>>> a.insert(0, 111)
>>> a
[111, 1, 23, 45, 1, -3434, 43624356, 234, 45]
# Count the length of the list
>>> a.count(45)
2
# Remove a element from the list
>>> a.remove(234)
>>> a
[111, 1, 23, 45, 1, -3434, 43624356, 45]
# Reverse a list
>>> a.reverse()
>>> a
[45, 43624356, -3434, 1, 45, 23, 1, 111]
# Append a list into another list
>>> b = [45, 56, 90]
>>> a.append(b)
>>> a
[45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90]]
>>> a[-1]
[45, 56, 90]
# Extend a into b
>>> a.extend(b) #To add the values of b not the b itself
>>> a
[45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90], 45, 56, 90]
>>> a[-1]
90
# Sort a list
>>> a.sort()
>>> a
[-3434, 1, 1, 23, 45, 45, 45, 56, 90, 111, 43624356, [45, 56, 90]]
# Delete a element in list
>>> del a[-1]
>>> a
[-3434, 1, 1, 23, 45, 45, 45, 56, 90, 111, 43624356]
# Basic Data Structure
>>> a = [1, 2, 3, 4, 5]
>>> a.append(1)
>>> a
[1, 2, 3, 4, 5, 1]
>>> a.pop(0)
1
>>> a.pop(0)
2
>>> a
[3, 4, 5, 1]
# List Comprehensions
>>> a = [1, 2, 3]
>>> [x ** 2 for x in a]
[1, 4, 9]
>>> z = [x + 1 for x in [x ** 2 for x in a]]
>>> z
[2, 5, 10]
while condition:
statement1
statement2
>>> n = 0
>>> while n < 11: ... print n ... n += 1 ... 0 1 2 3 4 5 6 7 8 9 10
>>> a = ['Python', 'is', 'powerful']
>>> for x in a:
... print x,
...
Python is powerful
>>> range(1, 5)
[1, 2, 3, 4]
>>> range(1, 15, 3)
[1, 4, 7, 10, 13]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10, 1, -2)
[10, 8, 6, 4, 2]
>>> for i in range(0, 5):
... print i
... else:
... print "Bye bye"
...
0
1
2
3
4
Bye bye
# Set: Another data-structure with no duplicate items
>>> a = set('abcthabcjwethddda')
>>> a
set(['a', 'c', 'b', 'e', 'd', 'h', 'j', 't', 'w'])
# Set Operations
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b # letters in both a and b
set(['a', 'c'])
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a
set(['a', 'c', 'b', 'e', 'd', 'h', 'j', 'q', 't', 'w'])
>>> a.add('p')
>>> a
set(['a', 'c', 'b', 'e', 'd', 'h', 'j', 'q', 'p', 't', 'w'])
>>> s = "I am Indian"
>>> s
'I am Indian'
>>> s = 'I am Indian'
>>> s = "Here is a line \
... splitted in two lines"
>>> s
'Here is a line split in two lines'
>>> s = "Here is a line \n split in two lines"
>>> s
'Here is a line \n split in two lines'
>>> print s
Here is a line
split in two lines
>>> s = """ This is a
... multiline string, so you can
... write many lines"""
>>> print s
This is a
multiline string, so you can
write many lines
# String Methods
>>> s = "We all love Python"
>>> s.split(" ")
['We', 'all', 'love', 'Python']
>>> x = "Nishant:is:waiting"
>>> x.split(':')
['Nishant', 'is', 'waiting']
>>> "-".join("GNU/Linux is great".split(" "))
'GNU/Linux-is-great'
>>> s = " abc\n "
>>> s.strip()
'abc'
>>> s = "faulty for a reason"
>>> s.find("for")
7
>>> s.find("fora")
-1
>>> s.startswith("fa") #To check if the string starts with "fa" or not
>>> s.endswith("reason") # True
>>> s = "manoj pandey"
>>> s.title()
'Sayan Chowdhury'
>>> z = s.upper()
>>> z
'MANOJ PANDEY'
>>> z.lower()
'manoj pandey'
>>> s = "I am A pRoGraMMer"
>> s.swapcase()
'i AM a PrOgRAmmER'
>>> f = open('foo.txt', 'w')
>>> f.write("First line")
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First line'
>>> f.readline()
''
>>> f = open('foo.txt', 'a')
>>> f.write('Second line')
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First lineSecond line'
>>> f = open('foo.txt', 'a')
>>> f.write("New line\n")
>>> f.write("One more new line")
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First lineSecond lineNew line\n'
>>> f.readline()
'One more new line'
>>> f.readline()
''
>>> f.close()
>>> f = open('foo.txt')
>>> f.readlines()
['First lineSecond lineNew line\n', 'One more new line']
>>> f = open('foo.txt', 'w')
>>> f.writelines(["1\n", "2\n"])
>>> f.close()
>>> f = open('foo.txt')
>>> f.readlines()
['1\n', '2\n']
>>> f.close()
"""
Bars Module
============
This is an example module with provide different ways to print bars.
"""
def starbar(num):
"""
Prints a bar with *
:arg num: Length of the bar
"""
print '*' * num
def hashbar(num):
"""
Prints a bar with #
:arg num: Length of the bar
"""
print '#' * num
def simplebar(num):
"""
Prints a bar with -
:arg num: Length of the bar
"""
print '-' * num
>>> import bars
>>> bars.hashbar(10)
##########
>>> from bars import simplebar, starbar >>> simplebar(10) >>> ----------
# NEVER DO THIS
>>> from bars import *
mymodule
|-- bars.py
|-- __init__.py
`-- utils.py
>>> help(str)
>>> import os
>>> os.getpid()
14875
>>> os.getcwd()
'/Users/rtnpro'
>>> os.chdir('/tmp')
>>> os.getcwd()
'/tmp'
def view_dir(path='.'):
"""
This function prints all files and directories in the given directory.
:args path: Path to the directory, default is current directory
"""
names = os.listdir(path)
names.sort()
for name in names:
print name,
>>> view_dir('/')
.readahead bin boot dev etc home junk lib lib64 lost+found media mnt opt
proc root run sbin srv sys tmp usr var
from BeautifulSoup import BeautifulSoup
import requests
url = 'http://pyvideo.org/category/33/pycon-us-2013/files'
req = requests.get(url)
data = req.text
soup = BeautifulSoup(data)
for link in soup.findAll('a', href=True):
# if link['href'].startswith(
#'http://s3.us.archive.org/nextdayvideo/psf/pycon2013/'):
if link.get('href', '').startswith(
'http://s3.us.archive.org/nextdayvideo/psf/pycon2013/'):
#print link.get('href')
print link.get('href').split('?Signature')[0]
try: run_a_workshop() print("Good job guys")
except PeopleDidNotLearn: print("We have to try harder")
finally: print("Heeeell yeah, It's finished")