% Created 2011-06-24 Fri 10:08 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} \usepackage{fixltx2e} \usepackage{graphicx} \usepackage{longtable} \usepackage{float} \usepackage{wrapfig} \usepackage{soul} \usepackage{textcomp} \usepackage{marvosym} \usepackage{wasysym} \usepackage{latexsym} \usepackage{amssymb} \usepackage{hyperref} \tolerance=1000 \usepackage[english]{babel} \usepackage{ae,aecompl} \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} \usepackage{listings} \lstset{language=Python, basicstyle=\ttfamily\bfseries, commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, showstringspaces=false, keywordstyle=\color{blue}\bfseries} \providecommand{\alert}[1]{\textbf{#1}} \title{} \author{FOSSEE} \date{} \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} \begin{document} \begin{frame} \begin{center} \vspace{12pt} \textcolor{blue}{\huge Conditionals} \end{center} \vspace{18pt} \begin{center} \vspace{10pt} \includegraphics[scale=0.95]{../images/fossee-logo.png}\\ \vspace{5pt} \scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ \scriptsize Funded by National Mission on Education through ICT\\ \scriptsize MHRD,Govt. of India\\ \includegraphics[scale=0.30]{../images/iitb-logo.png}\\ \end{center} \end{frame} \begin{frame} \frametitle{Objectives} \label{sec-2} At the end of this tutorial, you will be able to, \begin{itemize} \item Use if/else blocks. \item Use if/elif/else blocks. \item Use the Ternary conditional statement - C if X else Y. \end{itemize} \end{frame} \begin{frame} \frametitle{Pre-requisite} \label{sec-3} Spoken tutorial on - \begin{itemize} \item Basic Datatypes and Operators \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{\verb~if/elif~ ladder} \label{sec-4} \lstset{language=Python} \begin{lstlisting} if user == 'admin': # Do admin operations elif user == 'moderator': # Do moderator operations elif user == 'client': # Do customer operations \end{lstlisting} \end{frame} \begin{frame} \frametitle{Exercise 1} \label{sec-5} Given a number, num. Write an if else block to print num, as is, if it is divisible by 10, else print 10 * num. \end{frame} \begin{frame}[fragile] \frametitle{Solution 1} \label{sec-6} \lstset{language=Python} \begin{lstlisting} if num%10 == 0: print num else: print 10*num \end{lstlisting} \end{frame} \begin{frame} \frametitle{Exercise 2} \label{sec-7} Given a number, num. Write a ternary operator to print num, as is, if it is divisible by 10, else print 10 * num. \end{frame} \begin{frame}[fragile] \frametitle{Solution 2} \label{sec-8} \lstset{language=Python} \begin{lstlisting} print num if num%10 == 0 else 10*num \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Pass statement} \label{sec-9} a = raw\_input("Enter `c' to calculate and exit, `d' to display the existing results exit and `x' to exit and any other key to continue: ") \lstset{language=Python} \begin{lstlisting} if a == 'c': # Calculate the marks and exit elif a == 'd': # Display the results and exit elif a == 'x': # Exit the program else: pass \end{lstlisting} \end{frame} \begin{frame} \frametitle{Summary} \label{sec-10} In this tutorial, we have learnt to, \begin{itemize} \item Understand the conditional statements in Python. \item Use if/else statement. \item Use if/elif/else statement. \item Apply the ternary conditional statement - C if X else Y. \item Use ``pass'' statement. \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Evaluation} \label{sec-11} \begin{enumerate} \item Use conditional statements for the following. Given a variable ``time'', print ``Good Morning'' if it is less than 12, otherwise print ``Hello''. \vspace{10pt} \item Convert the if else ladder below into a ternary conditional statement. \lstset{language=Python} \begin{lstlisting} x = 20 if x > 10: print x * 100 else: print x \end{lstlisting} \end{enumerate} \end{frame} \begin{frame}[fragile] \frametitle{Solutions} \label{sec-12} \begin{enumerate} \item \lstset{language=Python} \begin{lstlisting} if time < 12: print "Good Morning" else: print "Hello" \end{lstlisting} \vspace{9pt} \item \lstset{language=Python} \begin{lstlisting} print x * 100 if x > 10 else x \end{lstlisting} \end{enumerate} \end{frame} \begin{frame} \begin{block}{} \begin{center} \textcolor{blue}{\Large THANK YOU!} \end{center} \end{block} \begin{block}{} \begin{center} For more Information, visit our website\\ \url{http://fossee.in/} \end{center} \end{block} \end{frame} \end{frame} \end{document}