Python 2.7: Basics: Variables, Strings, Date and Time

Variables

x = 10

This statement will create a variable called x which will store a number 10. By assigned value python understands that this variable will store numeric data. We can also store other data types, by assigning different types of values:

x_int = 1
x_float = 1.23
x_bool = True

Note that booleans should have an uppercase in the beginning. (True and not true, False and not false)

To print anything on the screen you can use the print function:

print x_float

Whitespaces

In python whitespaces have a significant meaning - they are used to structure code beyond the scope of indentation and formatting. Badly formatted code will result in IndentiationError.

Comments

Single-line comments

# my comment

Multi-line comments

""" my comment - mutline
line comment """

Math

Python will go beyond the standard math operators provided by the programming languages. In addition to operations like multiplication, addition, subtraction and division, python will provide you with some more interesting operators.

Let’s assume you want to raise 2 to a power of 3. This is how we can do it in python:

result = 2 ** 3

Exponentiation is performed in python by using ** operator.

Strings

Similar to any other language:

x_string = "John"

# espace character 
x_string = 'This is John\'s birthday'

String is actually an array of characters. You can directly access characters by using indexes:

x_string = "Python"

# will display the character t
print x_string[2]

String operators:

len() - returns the number of characters in a string

# will print number 5 as a number of characters in the word hello
print len("hello")

string.lower() - this method is used to convert all characters to lowercase

# output: hello
print "Hello".lower()

string.upper() - converts all characters to uppercase

# output: HELLO
print "Hello".upper()

str() - converts non-strings to strings

# creating a float variable pi
pi = 3.14

# converting pi to string
print str(pi)

String Concatenation

Strings can be concatenated using the summation operator +:

# output: Times of our life
print "Times " + " of  " + " our " + " life "

# output: My score is 50
x_int = 50
print "My score is " + str(x_int)

Another way to print strings by doing string replacement is to format them with %

# output: Jane and Mike were best friends since a long time
string_1 = "Jane"
string_2 = "Mike"
print "%s and %s were best friends since a long time" % (string_1, string_2)

This can be a more interesting exercise:

# input: for first name: [Tom], for last name: [Hanks]
# output: Dear Tom Hanks, please proceed to the registration counter

first_name = raw_input("What's your first name?")
last_name = raw_input("What's your last name?")
print "Dear %s %s, please proceed to the registration counter" (first_name, last_name)

Date and Time

The code below displays the current time

# output - will print current time in format of : 2016-11-10 18:18:31.379762
from datetime import datetime
now = datetime.now()
print now

# output: current year, e.g. 2016
print now.year

# output: current month, e.g. 11
print now.month

# output: current day, e.g. 10
print now.day

just like above, you can also use now.hour, now.minute and now.second