Programming with Python
Content to be discuss … • Variables • Operands and Expressions • Conditional Statements
Variables • Variables are the names given to data that we need to store and manipulate in our program. • For example ,to store the age of the user ,we can write a variable age and it is defined as below. age = 0 or age = 50 After we define age ,the program will allocate some memory to store this data. • Every time we declare a variable ,some initial value must be given to it. • We can also define multiple variables at a time. For ex: age, name=30,’Peter’ This is same as age=30 name=‘Peter’
• The user name can contain any letters (lower case or upper case) numbers or underscores(-) but the first character shouldn’t be a number • For Ex: userName,user_Name,user_Name2 etc.. Are valid. • But… 2user_name ,2userName ..are not valid • Variable names are case sensitive .username and userNAME are not same. • Normally two conventions are used in Python to denote variables. Camel case notation or use underscores . • Camel case is the practice of writing the compound words with mixed casing. For ex:”thisIsAvariableName” • Another is to use underscores (-). Some precautions
• This separates the words.For example: “user_Name_variable” • In the above example age=30,the ‘=‘ sign is known as Assignment Sign • So the statements x=y and y=x have different meanings in programming.
Basic Operators • Python accepts all the basic operators like + (addition), (subtraction),*(multiplication), / (division), //(floor division), %(modulus) and **(exponent). • For example : if x= 7,y=2 addition: x+y=9 subtraction:x-y=5 Multiplication:x*y=14 Division: x/y=3.5 • Floor division: x // y=3(rounds off the answer to the nearest whole number) • Modulus: x%y= 1(Gives the remainder when 7 is divided by 2 • Exponent : x**y=49(7 to the power of 2)
Additional assignment Operators • +=: x+= 2 is same as equal to x=x+2 • Similarly for subtraction x-=2 is same as x=x-2
Data Types • Integers are numbers with no decimal parts like 2 , -5 , 27, 0 , 1375 etc.. • Float refers to a number that has decimal parts. Ex: 1.2467 ; -0.7865 ; 125.876 • userHeight = 12.53 userWeight = 65.872 • String: String refers to text. Strings are defined within single quotes. • For Ex: Variable Name = ‘initialvalue’ • VariableName=“initial value” • userName=‘peter’ ,userSpouseName=“Mary” userAge =“35” here “35” is a string • Multiple substrings can be combined by using the concatenate sign(+) • Ex:”Peter”+ “Lee” is equivalent to the string “PeterLee”
Type Casting • Type casting helps to convert from one data type to another .i.e from integer to a string or vice versa. • There are three built in functions in Python int(); str() ; float() • To change a float to an integer type int(7.3256). The output is 7 • To change a string into integer int(“4”).The output is 4 • Ex: int(“RAM”) ----- returns syntax error • int(“3.7654”) -------returns syntax error • The str() function converts an integer or a float to a string. • Ex: str(82.1) The output is “82.1” • Ex: str(757) The output is “757” • Ex: str(Ram) The output is “Ram”
Conditional Statement • if statement • if… else statement • Nested if statement Python uses indentation as its method of grouping statements
Indentation • Indentation refers to the spaces at the beginning of a code line. • Python uses indentation to indicate a block of code. Example: if 7 > 3: print(“Seven is greater than three *** ") Python generates an error without indentation in a block of code.
Assignment Section Write a python program to : • find maximum between two numbers • find minimum between three numbers. • check whether a number is negative, positive or zero. • whether a number is divisible by 5 and 11 or not. • check whether a number is even or odd. • check whether a year is leap year or not. • input week number and print week day • input month number and print number of days in that month. • count total number of notes in given amount

Basics of Python Programming

  • 1.
  • 2.
    Content to bediscuss … • Variables • Operands and Expressions • Conditional Statements
  • 3.
    Variables • Variables arethe names given to data that we need to store and manipulate in our program. • For example ,to store the age of the user ,we can write a variable age and it is defined as below. age = 0 or age = 50 After we define age ,the program will allocate some memory to store this data. • Every time we declare a variable ,some initial value must be given to it. • We can also define multiple variables at a time. For ex: age, name=30,’Peter’ This is same as age=30 name=‘Peter’
  • 4.
    • The username can contain any letters (lower case or upper case) numbers or underscores(-) but the first character shouldn’t be a number • For Ex: userName,user_Name,user_Name2 etc.. Are valid. • But… 2user_name ,2userName ..are not valid • Variable names are case sensitive .username and userNAME are not same. • Normally two conventions are used in Python to denote variables. Camel case notation or use underscores . • Camel case is the practice of writing the compound words with mixed casing. For ex:”thisIsAvariableName” • Another is to use underscores (-). Some precautions
  • 5.
    • This separatesthe words.For example: “user_Name_variable” • In the above example age=30,the ‘=‘ sign is known as Assignment Sign • So the statements x=y and y=x have different meanings in programming.
  • 6.
    Basic Operators • Pythonaccepts all the basic operators like + (addition), (subtraction),*(multiplication), / (division), //(floor division), %(modulus) and **(exponent). • For example : if x= 7,y=2 addition: x+y=9 subtraction:x-y=5 Multiplication:x*y=14 Division: x/y=3.5 • Floor division: x // y=3(rounds off the answer to the nearest whole number) • Modulus: x%y= 1(Gives the remainder when 7 is divided by 2 • Exponent : x**y=49(7 to the power of 2)
  • 7.
    Additional assignment Operators •+=: x+= 2 is same as equal to x=x+2 • Similarly for subtraction x-=2 is same as x=x-2
  • 8.
    Data Types • Integersare numbers with no decimal parts like 2 , -5 , 27, 0 , 1375 etc.. • Float refers to a number that has decimal parts. Ex: 1.2467 ; -0.7865 ; 125.876 • userHeight = 12.53 userWeight = 65.872 • String: String refers to text. Strings are defined within single quotes. • For Ex: Variable Name = ‘initialvalue’ • VariableName=“initial value” • userName=‘peter’ ,userSpouseName=“Mary” userAge =“35” here “35” is a string • Multiple substrings can be combined by using the concatenate sign(+) • Ex:”Peter”+ “Lee” is equivalent to the string “PeterLee”
  • 9.
    Type Casting • Typecasting helps to convert from one data type to another .i.e from integer to a string or vice versa. • There are three built in functions in Python int(); str() ; float() • To change a float to an integer type int(7.3256). The output is 7 • To change a string into integer int(“4”).The output is 4 • Ex: int(“RAM”) ----- returns syntax error • int(“3.7654”) -------returns syntax error • The str() function converts an integer or a float to a string. • Ex: str(82.1) The output is “82.1” • Ex: str(757) The output is “757” • Ex: str(Ram) The output is “Ram”
  • 10.
    Conditional Statement • ifstatement • if… else statement • Nested if statement Python uses indentation as its method of grouping statements
  • 11.
    Indentation • Indentation refersto the spaces at the beginning of a code line. • Python uses indentation to indicate a block of code. Example: if 7 > 3: print(“Seven is greater than three *** ") Python generates an error without indentation in a block of code.
  • 12.
    Assignment Section Write apython program to : • find maximum between two numbers • find minimum between three numbers. • check whether a number is negative, positive or zero. • whether a number is divisible by 5 and 11 or not. • check whether a number is even or odd. • check whether a year is leap year or not. • input week number and print week day • input month number and print number of days in that month. • count total number of notes in given amount