PYTHON PRACTICE INTERACTIVE MODE (COMMAND LINE)

 You can use the basic mathematical operators: 

1 >>> 3 + 3 

 6

  >>> 3 - 3 

 0 

 >>> 3 / 3 

 1.0 

 >>> 3 / 2

1.5

>>> 3 * 3 

 9

>>> 3 ** 3 

 27

 >>> num = 3

>>> num = num - 1

 >>> print(num)

2

>>> num = num + 10

>>> print(num)

12

>>> num += 10

print(num)

22 

>>> num -= 12

>>> print(num)

10

>>> num *= 10

>>> num 

100

There’s also a special operator called modulus, % , that returns the remainder after integer division.  >>> 10 % 3 

 1 

One common use of modulus is determining if a number is divisible by another number. For example, we know that a number is even if it’s divided by 2 and the remainder is 0. 

 >>> 10 % 2 

0

>>> 12 % 2

0

Finally, make sure to use parentheses to enforce precedence. 

 >>> (2 + 3) * 5 

 25

>>> 2 + 3 * 5

 17

Strings Strings are used quite often in Python. Strings, are just that, a string of characters - which s anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. Python recognizes single and double quotes as the same thing, the beginning and end of the strings.

>>> "string list"

'string list'

>>> 'string list'

'string list'

 >>> "I ’cant do that"

'I ’cant do that'

>>> "I ’cant do that"

'I ’cant do that'

>>> "He said \"no\" to me" 

'He said "no" to me' 

Now you can also join (concatenate) strings with use of variables as well. 

 >>> a = "first" 

 >>> b = "last" 

 >>> a + b 

 'firstlast'  

If you want a space in between, you can change a to the word with a space after.

>>> a = "first "

>>> a + b

'first last'

There are different string methods for you to choose from as well - like upper(), lower(), replace(), and count(). upper() does just what it sounds like - changes your string to all uppercase letters

>>> str = 'woah!'

>>> str.upper()

'WOAH!'

>>> str = 'WOAH!'

>>> str.lower()

'woah!'

replace() allows you to replace any character with another character. 

 >>> str = 'rule' 

 >>> str.replace('r', 'm') 

 'mule' 

Finally, count() lets you know how many times a certain character appears in the string. 

 >>> number_list =['one', 'two', 'one', 'two', 'two'] 

 >>> number_list.count('two') 

 3

                              Lists

 Lists are containers for holding values. 

 >>> fruits = ['apple','lemon','orange','grape'] 

 >>> fruits 

 ['apple', 'lemon', 'orange', 'grape']

To access the elements in the list you can use their associated index value. Just remember that the list starts with 0, not 1. 

 >>> fruits[2]‘’ 

 orange

If the list is long and you need to count from the end you can do that as well. 

 >>> fruits[-2]‘’ 

 orange

Now, sometimes lists can get long and you want to keep track of how many elements you have in your list. To find this, use the len() function. 

 >>> len(fruits) 

 4 

                              Dictionaries 

A dictionary optimizes element lookups. It uses key/value pairs, instead of numbers as placeholders. Each key must have a value, and you can use a key to look up a value. 

 >>> words = {'apple': 'red','lemon': 'yellow'} 

 >>> words 

 {'apple': 'red', 'lemon': 'yellow'} 

 >>> words['apple'] 

 'red' 

 >>> words['lemon'] 

 'yellow'

This will also work with numbers. 

 >>> dict = {'one': 1, 'two': 2} 

 >>> dict 

 {'one': 1, 'two': 2}


Conditional statement-

Prog 1

1.     >>> age=20

>>> if age>=18:

        print(‘You are eligible to vote.’)

output:- You are eligible to vote.


Prog 2

2.     age=15

if  age>=18:

     print(‘you are eligible to vote.’)

else:

     print(‘you cannot vote this time.)

Output:- You cannot vote this time.


Prog 3

 a=int(input(“Enter first number:”));

b=int(input(“Enter second number:”));

c=int(input(“Enter third number:”));

if a>b and a>c:

           print(“First number is the largest.”);

elif b>a and b>c:

          print(“second number is the largest.”);

elif c>a and c>b:

           print(“third number is the largest.”);

else

          print(“I don’t know”);


 

 

Comments

Popular posts from this blog

CLASS 8 DATA TYPES IN PYTHON

COMPUTER PRACTICAL CLASS VII