The Geeky Way - Python//thegeekyway.com/2014-02-26T17:44:00+05:30Conditional Statements in Python2014-02-26T17:44:00+05:302014-02-26T17:44:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-02-26:/conditional-statements/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg" title="Conditional Statements in python"><img alt="Conditional Statements in python" src="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg"></a></p>
</div>
<p>In this tutorial we will discuss about conditional statements in python. Conditional statements are used to perform different computation based on the <strong>Boolean expression</strong> which turns out to be true or false . <strong>Boolean expression</strong> is on which these conditional statement works. Various Conditional statement used are:</p>
<ul>
<li>If</li>
<li>Else</li>
<li>Else if …</li></ul><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg" title="Conditional Statements in python"><img alt="Conditional Statements in python" src="//thegeekyway.com/uploads/images/2014/02/Conditional_Statements1.jpg"></a></p>
</div>
<p>In this tutorial we will discuss about conditional statements in python. Conditional statements are used to perform different computation based on the <strong>Boolean expression</strong> which turns out to be true or false . <strong>Boolean expression</strong> is on which these conditional statement works. Various Conditional statement used are:</p>
<ul>
<li>If</li>
<li>Else</li>
<li>Else if</li>
</ul>
<p><strong>If</strong> condition</p>
<p>The general syntax for using if statement is</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="ss">(</span><span class="nv">Boolean</span><span class="w"> </span><span class="nv">expression</span><span class="ss">)</span><span class="w"> </span>:
<span class="w"> </span><span class="nv">code</span><span class="w"> </span>#<span class="nv">indentation</span><span class="w"> </span><span class="nv">is</span><span class="w"> </span><span class="nv">required</span>
</code></pre></div>
<p><strong>if</strong> is a keyword used for if statements ,it is a followed by a boolean expression which is evaluated . Now question arises is why space is given in front when we have written code statement . This is because as no brackets are used to hold the computations after the if statement as this is a case in c++,java which uses curly brackets {}. The symbol : signifies that from here if block starts .</p>
<p>In Python indentation is given rather than using brackets . If we not give the indentation compiler will give an error in case when no line is there in the if block .</p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">2</span><span class="c1">;</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">==</span><span class="mi">2</span><span class="ss">)</span>:
<span class="nv">print</span><span class="w"> </span><span class="nv">a</span>
<span class="k">else</span>
<span class="nv">print</span><span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"><img alt="Screenshot-7" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"></a></p>
</div>
<p>If statement can be used with else statements and can be used alone . If the condition evaluates to true then code inside if block is executed otherwise it is skip to the next line.</p>
<p><strong>else</strong> condition</p>
<p>The syntax of the else statement is</p>
<div class="highlight"><pre><span></span><code><span class="k">else</span><span class="w"> </span><span class="o">:</span>
<span class="w"> </span><span class="n">code</span>
</code></pre></div>
<p>Here else is a keyword used and there are no brackets used as in if statement ,else is also followed by the code segment and indentation is required. Else statement are followed by if statement and cant be used alone . Compiler checks the if block first if Boolean expression doesn't come out to be true ,code inside else statement gets executed.</p>
<p><strong>Example</strong></p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">2</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">==</span><span class="mi">2</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="k">else</span><span class="w"> </span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<p>In this example a has been assigned a value 2 , after that value of a is checked by using if statement . The statement (a==2) means a is compared to the value 2 if it comes to be true if statement will execute otherwise else statement will execute. The output for this case will be "YES".</p>
<p><strong>Elif</strong></p>
<p>To avoid multiple if else statements we use else if .It is just like a else followed by a if .elif is a keyword used for else-if statemnts . This example will tell the use of elif keyword.</p>
<div class="highlight"><pre><span></span><code><span class="nv">a</span><span class="o">=</span><span class="mi">9</span>
<span class="k">if</span><span class="ss">(</span><span class="nv">a</span><span class="o">%</span><span class="mi">2</span><span class="o">==</span><span class="mi">0</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="nv">elif</span><span class="w"> </span><span class="ss">(</span><span class="nv">a</span><span class="o">%</span><span class="mi">3</span><span class="o">==</span><span class="mi">0</span><span class="ss">)</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="s2">"YES"</span>
<span class="k">else</span>
<span class="w"> </span><span class="s2">"NO"</span>
</code></pre></div>
<p>Here first if condition is checked ,if it comes out to be false it goes to elseif block and if that also comes to be false ,it finally goes to else block.</p>
<p>\% is an operator used to find modulus of a number .It find the remainder when two numbers are divided.</p>
<p>Any doubts related to conditional statement can be asked in comment section .In next tutorial we will discuss about looping .</p>Variables in PYTHON2014-01-31T21:07:00+05:302014-01-31T21:07:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-01-31:/variables-in-python/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/variables.jpg" title="Variables in python"><img alt="Variables in Python" src="//thegeekyway.com/uploads/images/2014/01/variables.jpg"></a></p>
</div>
<h2>Variables</h2>
<p>Variables are used for storing data in the form of integer,char ,boolean etc.They act as a storage medium for us . Based on the datatype of variable interpreter allocates the memory required by that datatype. There are 4 types of variables.</p>
<ul>
<li>Integer</li>
<li>String</li>
<li>float</li>
<li>complex</li>
</ul>
<p>Values are assigned …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/variables.jpg" title="Variables in python"><img alt="Variables in Python" src="//thegeekyway.com/uploads/images/2014/01/variables.jpg"></a></p>
</div>
<h2>Variables</h2>
<p>Variables are used for storing data in the form of integer,char ,boolean etc.They act as a storage medium for us . Based on the datatype of variable interpreter allocates the memory required by that datatype. There are 4 types of variables.</p>
<ul>
<li>Integer</li>
<li>String</li>
<li>float</li>
<li>complex</li>
</ul>
<p>Values are assigned to variables by using (=) sign..There is no need to write keyword when initializing a variable as interpreter itself asisign it according to the value given .</p>
<h2>Integer</h2>
<p>Integer is used for storing integer data . The keyword for integer variables is int .</p>
<div class="highlight"><pre><span></span><code>a=10
</code></pre></div>
<p>The range of integer is (2\^32) and get promoted to long when a number greater than (2\^32)</p>
<h2>String</h2>
<p>Strings are used for storing character literals .When we want to save some names of person or for any character related data we use strings.</p>
<div class="highlight"><pre><span></span><code>a="simer"
</code></pre></div>
<p>We can apply various operations on strings . To print letters of string we can give the index of the letter and print it .Index of a string always start from 0.</p>
<div class="highlight"><pre><span></span><code>a="simer"
print a[0]
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-55.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-55.png"></a></p>
</div>
<p>0 is the index of the 1 letter of the string ,similarly 1 idex will be of 2 letter and so on.</p>
<p>We can also give the range of letters which we want to print of the given string .These can be given as :</p>
<div class="highlight"><pre><span></span><code>a="simer"
print a[0:] # to print all the letter from index 0
print a[1:3] # to print letter starting from index 1 to index 3
print a[:4] # to print first 4 letter of string
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-6.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-6.png"></a></p>
</div>
<h2>Float</h2>
<p>float variables are used to store floating point values . They consist of two parts mantissa and exponent.</p>
<p>eg 2*10^-1 ^</p>
<p>Here 2 is the mantissa , -1 is the exponent and 10 is the base of the exponent.</p>
<div class="highlight"><pre><span></span><code>a=2.5
</code></pre></div>
<h2>Complex</h2>
<p>Complex numbers are represented by a+bj where a is the real part of number and b is the imaginary part of the number .They are used to repreent real floating point numbers.</p>
<div class="highlight"><pre><span></span><code>a=5+10i
print a
</code></pre></div>
<h2>Multiple Assignnmet</h2>
<p>We can assign multiple variables at the same time to same value. This will save time by not initializing the variable every time.</p>
<div class="highlight"><pre><span></span><code>a=b=c=20
</code></pre></div>
<h2>Conversions</h2>
<p>We can convert datatype of a variable to another datatype .</p>
<p>Integer to float or vice versa</p>
<p>We can convert a floating point number to an integer or a integer to floating point by using int and float keywords with that variable.</p>
<div class="highlight"><pre><span></span><code>a=123.5
print a
a=int(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-8.png"><img alt="Screenshot-8" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-8.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=123
print a
a=float(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"><img alt="Screenshot-7" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-7.png"></a></p>
</div>
<p>integer to string or vice versa</p>
<p>We can convert integer to sting and strings to integers by using int and str keywords .Numeric strings can be converted to integers only,alphabetic strings will not be converted.</p>
<div class="highlight"><pre><span></span><code>a="1234"
print a
a=int(a)
print a+2
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-57.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-57.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=1234
print a
a=str(a)
print a,a+2 #a+2 to check that is been converted to string
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-62.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-62.png"></a></p>
</div>
<p>Complex to string or vice versa</p>
<p>Complex can only be converted to string datatype and only numeric string can be converted to complex datat type ,keyword used for complex is complex.</p>
<div class="highlight"><pre><span></span><code>a="123"
a=complex(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-61.png"><img alt="Screenshot-6" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-61.png"></a></p>
</div>
<div class="highlight"><pre><span></span><code>a=5+8j
a=str(a)
print a
</code></pre></div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-56.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-56.png"></a></p>
</div>
<p>Any queries related to this tutorial can be asked and in next tutorial we will discuss about conditional statements in python.</p>PYTHON - Getting Started2014-01-15T13:03:00+05:302014-01-15T13:03:00+05:30Simerdeep Singhtag:thegeekyway.com,2014-01-15:/python/<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"><img alt="1624592_800549696627341_1247213667_n" src="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"></a></p>
</div>
<hr>
<p style="color: #000000"> </p>
<p style="color: #000000">Why Python ?</p>
<hr>
<p style="color: #000000">Python is a **high-level programming** language whose syntax is said to be clear and expressive. Python has a large standard library. Python is an easy to understand language we can learn this language in a few days and for making any gaming software we should must have good …</p><div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"><img alt="1624592_800549696627341_1247213667_n" src="//thegeekyway.com/uploads/images/2014/01/1624592_800549696627341_1247213667_n.jpg"></a></p>
</div>
<hr>
<p style="color: #000000"> </p>
<p style="color: #000000">Why Python ?</p>
<hr>
<p style="color: #000000">Python is a **high-level programming** language whose syntax is said to be clear and expressive. Python has a large standard library. Python is an easy to understand language we can learn this language in a few days and for making any gaming software we should must have good knowledge of **GUI development** using that language. GUI development using Python is very easy and understandable as compared to other language like C, C++or Java.</p>
<p style="color: #000000">Starting Python</p>
<hr>
<p style="color: #000000">Creating a simple Hello world program</p>
<ol>
<li><p style="color: #000000">We need a python compiler to compile python programs which can be downloaded from <strong>www.python.org</strong></p></li>
<li><p style="color: #000000">Writing a program in <strong>text file</strong> or <strong>console</strong></p></li>
</ol>
<p style="color: #000000">**Text File**</p>
<div class="highlight"><pre><span></span><code>print 'hello world'
</code></pre></div>
<div style="text-align:center">
<p>Compiling and running the program are done using single command <strong>python ‘filename’</strong> <a href="//thegeekyway.com/uploads/images/2014/01/screen1.png"><img alt="screen1" src="//thegeekyway.com/uploads/images/2014/01/screen1.png"></a></p>
</div>
<p style="color: #000000">**Console**</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/screen2.png"><img alt="screen2" src="//thegeekyway.com/uploads/images/2014/01/screen2.png"></a></p>
</div>
<p style="color: #000000">To get exit from command line interface just type the command **exit()** .</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-5.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-5.png"></a></p>
</div>
<p style="color: #000000">To get help about some methods related to python or the keywords we can type help in command line interface .Follow these steps</p>
<ol>
<li><p style="color: #000000">Type python in command line to start the interpreter of python.</p></li>
<li><p style="color: #000000">Type <strong>help</strong> .</p></li>
<li><p style="color: #000000">To get help about <strong>keywords</strong> or <strong>modules</strong> available in python just type ethe word keyword.</p></li>
</ol>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-51.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-51.png"></a></p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-52.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-52.png"></a></p>
</div>
<p style="color: #000000">All these keywords will be discussed in the coming tutorials one by one .As we can see there are nearly **31 keywords** available in python 2.6.5.</p>
<p style="color: #000000">**Print statement**</p>
<hr>
<p style="color: #000000">Print statement is used to print the data in python.Printing a statement in python is just so simple as we dont need to add any libraries for using print command as which we have to do in other languages.We have seen how to print "hello world", we can print same "hello world" in different way by writing 2 words in different inverted commas and printing it .To print 2 words at the same time we use comma operator between them.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/01/Screenshot-54.png"><img alt="Screenshot-5" src="//thegeekyway.com/uploads/images/2014/01/Screenshot-54.png"></a></p>
</div>
<p style="color: #000000">**Variable** are printed by just writing the name of variables and no inbverted commas are required .Variables will be covered in next tutorial ,there we will be showing how to print the variables . </p>
<p style="color: #000000">Comments</p>
<hr>
<p style="color: #000000">Comments are used to document our code . We write comments in python by using **symbol \#** and writing any expression ,line,or character in front of the \#.Compiler ignores the words or expression in comment.</p>
<p style="color: #000000">There are two types of comments available in python:</p>
<h3><p style="color: #000000"><strong>Single line comments</strong></p></h3>
<p style="color: #000000">\# is used to write the single line comments . </p>
<div class="highlight"><pre><span></span><code>print 'hello world' #will print hello world
</code></pre></div>
<p style="color: #000000">**will print hello world** is a comment and will be ignored by the compiler .</p>
<h3><p style="color: #000000"><strong>Multiple Line comments</strong></p></h3>
<p style="color: #000000">To write multiline comments the symbol is """ """ .</p>
<div class="highlight"><pre><span></span><code>print 'hello world' """ this is a
multiline comment """
</code></pre></div>
<h3><p style="color: #000000"> Any query related to this tutorial can be asked .In our next Tutorial we will discuss about variables .</p></h3>